应用程序后台运行时,NSTimer的行为是什么? [英] What is an NSTimer's behavior when the app is backgrounded?

查看:94
本文介绍了应用程序后台运行时,NSTimer的行为是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道,当您为应用程序添加背景时,计时器会停止运行.但是,当您从后台回来时,该怎么办?计时器是否保持其原始的fireDate?

I know that when you background an app, the timer stops running. However, what is the behavior when you come back from the background? Does the timer maintain its original fireDate?

我遇到了一个问题:从后台返回时,NSTimer设置为在十分钟内触发,有时我会立即触发定时器,甚至在调用applicationWillEnterForeground:之前.当它不触发并调用applicationWillEnterForeground:时,我有一些逻辑可以通过将计时器的fireDate与当前日期进行比较来检查10分钟是否已经过去,但是,即使已经过去了10分钟,也似乎总是如此给我大约从当前日期算起的525秒的值.

I've run into a bit of an issue where upon returning from the background with an NSTimer set to fire in ten minutes, sometimes I'll immediately have the timer fire before applicationWillEnterForeground: is even called. When it doesn't fire, and applicationWillEnterForeground: is called, I have some logic to check if the 10 minutes has passed by comparing the timer's fireDate to the current date, however, even if 10 minutes has passed, it always seems to give me a value of about 525 seconds in the future from the current date.

我的理论如下:

  • NSTimer将在从后台返回时触发计时器,只要您没有错过比原定计划时间更多的计划时间. (在这种情况下,我们将计时器安排为10分钟,只要您在后台停留10至20分钟,它将在重新进入该应用后触发.)

  • An NSTimer will fire the timer upon coming back from the background, as long as you haven't missed the scheduled time by more than what was originally scheduled. (In this case, we schedule the timer for 10 minutes, as long as you're in the background for between 10-20 minutes, it will fire upon re-entering the app.)

如果超过了该时间,它不会触发,而是在您返回时为将来的同一时间段安排另一个计时器. (又名,如果您在后台使用10分钟计时器在后台运行了20分钟以上,那么当您返回时,它会将计时器安排在从返回应用程序之时起未来10分钟内使用.)

If more than that has passed, it does not fire and instead schedules another timer for the same time period in the future when you return. (A.K.A. If you've been in the background for 20+ minutes with a 10 minute timer, when you get back, it schedules the timer for 10 minutes in the future from the time you return to the app.)

有人对此行为有任何文档吗?我的理论纯粹是基于观察,是关于文档的背景知识,或者缺少背景信息,或者我找不到它.

Does anyone have any documentation on this behavior? My theories are based purely off observation, the documentation on what happens when backgrounding is either woefully lacking, or I haven't been able to find it.

推荐答案

您正确地观察了NSTimer的行为.

You observed correctly how an NSTimer behaves.

如果现在创建一个计时器,该计时器应每10分钟触发一次,它将尝试从现在开始分别触发10分钟,20分钟,30分钟等.如果应用程序在一分钟后进入睡眠状态,并在12分钟后醒来,计时器将立即启动,然后在20分钟后再次启动(计时器启动后不是十分钟,而是原始时间后二十分钟).

If you create a timer right now that should fire every ten minutes, it will try to fire ten minutes, 20 minutes, 30 minutes and so on from now. If the app goes to sleep after one minute and wakes up after 12 minutes, the timer will fire immediately, and then again at 20 minutes (not ten minutes after the timer firing, but 20 minutes after the original time).

但是,如果计时器完全错过了一个事件,然后又延迟了下一个计时器事件,例如,如果您在29分钟后醒来,则会丢弃10分钟事件,并尽快触发20分钟事件(29分钟后),并且30分钟后会触发30分钟事件.

But if the timer misses one event completely and then is late for the next timer event as well, say if you wake up after 29 minutes, then the 10 minute event is dropped, the 20 minute event is fired as soon as possible (after 29 minutes), and the 30 minute event is fired after 30 minutes.

这篇关于应用程序后台运行时,NSTimer的行为是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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