13

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?

fuzzygoat
  • 26,573
  • 48
  • 165
  • 294
  • Yup, I think you've got it. It's good practice to shut down anything you have running, like a timer, if your app is going to get backgrounded assuming of course that that makes sense in the context of what your app does. :-) – Mark Granoff May 05 '11 at 16:59

1 Answers1

20

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.

ughoavgfhw
  • 39,734
  • 6
  • 101
  • 123
  • 3
    how can i disable the NStimer in appdelegate class as i am using it in some other class?? – hemant Sep 14 '11 at 12:15
  • I am running an NSTimer in my appdelegate to track user activity (so they can be logged off when they are inactive for too long). Does this mean that I can just leave the timer running when the app goes into the background and the timer will fire when they start the app, thus giving me the desired behavior? That is, when they open the app after a long time, my inactivity timer will fire and they'll be logged out? – mtmurdock Mar 14 '12 at 15:35
  • 1
    @mtmurdock Remember that you will have no way of knowing if the user quits your application in the background, and then your timer won't fire. If you remove some objects for backgrounding which the timer uses, you also won't know if you can create them before the timer fires. It might be easier to simply save the time the app was backgrounded and compare it when it is reopened. – ughoavgfhw Mar 14 '12 at 19:42
  • @ughoavgfhw That is some good advice, I hand't thought of those things. Luckily for me I don't have to worry about those scenarios due to the nature of my app, but I will keep that in mind for the future. – mtmurdock Mar 14 '12 at 22:14
  • @hemant You could make the timer accessible through some method/property and access your delegate via `[[UIApplication sharedApplication] delegate]`. Or alternatively you could simply make a method like `disableTimer` on your app delegate and call it in a similar fashion. – mtmurdock Mar 14 '12 at 22:16