How do people deal with a scheduled NSTimer when an app is in the background?
Let's say I update something in my app every hour.
updateTimer = [NSTimer scheduledTimerWithTimeInterval:60.0*60.0
target:self
selector:@selector(updateStuff)
userInfo:nil
repeats:YES];
When in the background, this timer obviously doesn't fire(?). What should happen when the user comes back to the app..? Is the timer still running, with the same times?
And what would would happen if the user comes back in over an hour. Will it trigger for all the times that it missed, or will it wait till the next update time?
What I would like it to do is update immediately after the app comes into the foreground, if the date it should have fired is in the past. Is that possible?
create Global uibackground task identifier.
UIBackgroundTaskIdentifier bgRideTimerTask;
now create your timer and add BGTaskIdentifier With it, Dont forget to remove old BGTaskIdentifier while creating new Timer Object.
Here this will work for me even when app goes in background.ask me if you found new problems.
You need to add your timer in current run loop.
This post suggests that things aren't quite as clear as that. You should invalidate your timers as your app goes into the background and restart them when it comes back to foreground. Running stuff while in the background might be possible but then again it might get you killed...
You can find good documentation about the iOS multitasking approach here.