在睡眠 / PowerNap中防止NSTIMER发射
当MacBook的盖子关闭并且机器应该入睡时,如何防止(略有非标准)NSTIMER向MacOS射击MacOS?或更具体地说,我如何选择退出电力小睡。
这是我用来设置计时器的代码:
self.timer = [NSTimer scheduledTimerWithTimeInterval:60 * 5
target:self
selector:@selector(scheduleTimerFired:)
userInfo:nil
repeats:YES];
for (NSString *mode in @[NSModalPanelRunLoopMode, NSDefaultRunLoopMode, NSRunLoopCommonModes]) {
[[NSRunLoop currentRunLoop] addTimer:self.timer
forMode:mode];
};
睡眠期间启动会导致问题,因为此计时器开始使用OAuth代币与远程服务器进行通信。这些令牌可能会在此过程中得到刷新。由于Mac在睡眠中,因此用户的钥匙扣似乎已被锁定,从而防止刷新的令牌写入它(错误:不允许使用用户互动)。
因此,我宁愿在睡眠模式下完全没有计时器执行计时器。
How can I prevent a (slightly non-standard) NSTimer from firing on macOS, when the MacBook's lid is closed and the machines is supposed to be at sleep? Or more specifically, how can I opt-out of Power Nap.
Here is the code I use to setup the timer:
self.timer = [NSTimer scheduledTimerWithTimeInterval:60 * 5
target:self
selector:@selector(scheduleTimerFired:)
userInfo:nil
repeats:YES];
for (NSString *mode in @[NSModalPanelRunLoopMode, NSDefaultRunLoopMode, NSRunLoopCommonModes]) {
[[NSRunLoop currentRunLoop] addTimer:self.timer
forMode:mode];
};
Firing during sleep causes issues, as this timer starts communication with a remote server using OAuth tokens. These tokens might be refreshed in the process. Since the Mac is at sleep, the users's keychain seems to be locked, which prevents refreshed tokens from being written to it (error: user interaction not allowed).
Therefore, I'd rather not have the timer executed at all when in sleep mode.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论