在睡眠 / PowerNap中防止NSTIMER发射

发布于 2025-01-25 02:31:03 字数 798 浏览 3 评论 0原文

当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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文