NSTimers 在后台运行?
我的印象是,在应用程序调用 applicationWillResignActive
后,NSTimer 根本不起作用。然而,我似乎现有的 NSTimers (即在应用程序退出活动之前创建的 NSTimers)将继续运行,并且它唯一的新 NSTimers 无法在此状态下安排,任何人都可以确认这一点吗?
我还假设,当您的应用程序调用 applicationWillResignActive
时,您应该禁用任何 NSTimers 并在调用 applicationDidBecomeActive
时再次启动它们,这很好(Apple 似乎也这么说),这有道理吗?
I was under the impression that NSTimer did not work at all after an application calls applicationWillResignActive
. I seems however that existing NSTimers (i.e. ones created before the application resigned active) will continue to run and its only new NSTimers that can't be scheduled in this state, can anyone confirm this?
I am also assuming that its good (and Apple seems to say this too) that when your application calls applicationWillResignActive
you should disable any NSTimers and start them again when applicationDidBecomeActive
is called, does that make sense?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当应用程序处于非活动状态但仍处于前台时(例如当用户收到推送通知或按下睡眠按钮时),您的应用程序仍然完全运行。您创建的任何未停止的计时器都会正常触发。但是,当您的应用程序进入后台时,如果您没有注册运行后台线程,则所有执行都会停止。如果计时器到了触发的时间,它就不会发生,因为运行循环没有运行。然而,当您的应用程序重新打开时,任何应该在后台触发的计时器都将立即触发。苹果建议在
applicationWillResignActive
中进行清理,这样当用户不专注于您的应用程序时您就不需要做很多工作,但您肯定希望在进入后台之前禁用计时器,这样它们就不会'当您的应用程序重新打开时,所有这些都会一个接一个地触发。When an application is inactive, but still in the foreground (such as when the user gets a push notification or presses the sleep button) your application is still running completely. Any timers you have created which you don't stop will fire as normal. However, when your application goes to the background, if you are not registered to run a background thread all execution is stopped. If it is time for a timer to fire, it will not happen because the run loop is not running. When your application is reopened, however, any timers which were supposed to fire while it was in the background will all be fired immediately. Apple suggests doing cleanup in
applicationWillResignActive
so that you are not doing a lot of work when the user is not focused on your application, but you definitely want to disable timers before going to the background so that they don't all fire one after the other when your application is reopened.