NSTimers在后台运行? [英] NSTimers running in background?

查看:168
本文介绍了NSTimers在后台运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的印象是,NSTimer在应用程序调用 applicationWillResignActive 后根本没有工作。但我似乎现有的NSTimers(即在应用程序之前创建的活动)将继续运行,并且其唯一新的NSTimers无法在此状态下安排,任何人都可以确认这一点?

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?

我还假定它的好处(当苹果似乎也这样说),当你的应用程序调用 applicationWillResignActive 时,你应该禁用任何NSTimers并重新启动它们 applicationDidBecomeActive 被调用,是否有意义?

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?

推荐答案

是不活动的,但仍然在前台(例如当用户获得推送通知或按下睡眠按钮),您的应用程序仍然完全运行。您创建的任何未停止的计时器将正常触发。但是,当您的应用程序转到后台时,如果您没有注册运行后台线程,则所有执行都停止。如果是定时器触发的时间,它不会发生,因为运行循环不运行。当您的应用程序重新打开时,任何应该在后台触发的计时器将立即被触发。 Apple建议在 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.

这篇关于NSTimers在后台运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆