0

Playing around with Timers, and trying to make them behave right when app is sent to background, killed etc.

Judging by the generated comments in xcode, you should disable/invalidate timers on resignActive/enterBackground events.

However, i havent done that yet, but checked what happens to my timer when i click the home button and then re-enter the app.

I basically have a method in my view that the timer triggers every second:

NSLog(@"workedTimTimerTick workedTime; %@", timeString);

And when i exit the app, the output stops, when i re-enter the app, the output starts again...

Since i'm not doing anything code-wise to the timer on those lifecycle events, how come it stops getting called?

Input appreciated!

Mathias
  • 3,879
  • 5
  • 36
  • 48

1 Answers1

2

Your app is suspended when it enters background mode (Application States and Transitions).

Your timer won't fire when the app is in background and the time spent in background isn't taken into account for the timer delay.

Jilouc
  • 12,684
  • 4
  • 46
  • 43
  • Thanks man, as i thought then. Can you tell me why the generated comments in xcode say that i manually should disable the timer on the resignactive-call? Seems unneccessary if the os suspends the timer anyway. – Mathias Sep 19 '11 at 19:22
  • @Mathias I guess it could be memory related (timers retain their target until fired or invalidated). Also firing it after the remaining delay when the user comes back may not be relevant (especially after a long background time). – Jilouc Sep 19 '11 at 20:11
  • Yeah, i ended up doing that, but for another reason :) i have an appdelegate that switches between three views and i release the old view and create the new one every time i switch. I noticed that the view with the timer doesn't get released unless i also kill the timer, not surprising of course. – Mathias Sep 20 '11 at 08:12