Does code runs in background mode in iOS based on timers? - ios

I started a timer when app goes into background when selector method called app got crashed in background. I am not sure does code works in defined conditions.

In background mode, when your app is not running, timers will just not fire. A timer will fire at the earliest time it can (for example just after your app is activated again). If a repeating timer should have fired more than once, only the last "fire" will happen.
This shouldn't make your app crash; whatever goes wrong is something else.

Timers only run in the background if you app supports running in the background, like when tracking location, playing music, etc.

Related

In a iOS app, if a timer is scheduled to fire and the app is in background, am I certain that it will fire as soon as the app goes foreground again?

I a swift iOS app, I have a timer that fires every day (86400 seconds). I have tested the behaviour of the timer when the app is in background, and it seems to me that in that situation, the timer will fire only when the app goes into foreground again. Am I guaranteed of this behaviour ? I want to be certain the timer does fire. It is fine for me if it fires when the app goes in foreground again, as long as it fires then. Thanks
The short answer is that an application can continue "executing" in the background indefinitely for only a limited number of reasons. Aside from these specific reasons, the app can ask for short periods of time to continue executing in the background. Once these periods are over you tell iOS you are done, in which case the app is suspended, or iOS will eventually forcibly terminate your app (it depends on resources).
So, firing a timer is not one of those specific reasons. However, the processing you are doing with the timer could be!
Your app will briefly be in the background on its way to being suspended. When it is suspended, it is not executing anything -- including the timer.
If your app is terminated (swiped from memory or shutdown by iOS), it isn't coming to the foreground, it is being launched again. And your timer is firing because you are launching your app.
If your app stays suspended, it will come to the foreground. And your timer will fire because of the time interval involved.
Either way you can guarantee that your timer fires.

How to keep a timer counting when app reach background

I prepared a CountDown timer for Pomodoro technique. I would like to know how don't pause the app when it reach a background. I have a method which update UILabel from 20min to 0 by 1sec. When Timer reach 0 it should play the sound and vibrate device. All works fine when app is launched in foreground, but how to do it at background? Is it possible to track timer change when app is in background mode?
BR
iMat
The short answer is no. A timer on a VC will not continue to run when the app is in the background because it goes into suspended mode.
You could schedule a local notification to fire when the app is in the background, but as far as updating the UI label, you'll have to update that when the user comes back into the app.
Invalidate the timer when the app goes to background. Store the remaining time remainingTime and current time backgroundTime. (You can get the current time using Date())
Compare the current time backToForegroundTime when the app comes back with backgroundTime. Subtract them to get the time elapsed timeElapsed.
If timeElapsed is less than the remainingTime, subtract that amount from remainingTime and create the timer again with the new duration.
You can use my approach from this gist. Just create repeating timer and update what ever you want in repeating block, and handle timer finishing in other block on main queue or background queue!
Glad to help with questions!
Apple has defined a specific set of tasks, an app can perform when in background.
Running a timer, unfortunately, is not one of them.
Read Background Execution section of app programming guide for more details.
Most apps, intending to continue to execute code in background, implement one of the allowed long running background modes, even if it is not required for your apps actual functionality, and use them to execute their code.
But be ware, you will be doing something apple specifically asks you not to do. This could result in app store rejection if found.

Application functionality changes when run on device

I've found that my application stops its timer when the device goes to the lock screen on actual device, but it works perfectly on the simulator.
Is that because I haven't released the app, so an actual device won't let me keep working on the lock screen? Or is it a bug in my code?
I've written an NSLog() statement to monitor my application's timer action, it keeps working when the simulator goes to the lock screen,
but on an actual device it stops.
I'm so frustrated because I don't know why there are these differences between the device and the simulator, and I don't know how to solve it.
What you need to do is implement applicationWillEnterForeground and applicationDidEnterBackground to persist the state of your timer, and resume it whenever the app opens again.
You may also want to use UILocalNotification to schedule a notification when your timer finishes.
Here is another post about using UILocalNotification with timers that you may find useful.

IOS: forcing update before app becomes visible

I have a timer in my app, and I want it to appear to be running while my app is on the background. I'd like that if the user presses the home button when the timer shows, for instant "01:11:11" and then goes back to the app and it becomes visible to him 10 minutes later, to see the timer as "01:01:11", however I get a split second where it shows the last state when the app went to the background ("01:11:11") before it starts updating from the correct time.
I assumed that I could correct this by updating the state of my timer in "applicationDidBecomeActive" and it did work on my simulator in Xcode but not on my Ipad.
I'm using cocos2d for my drawing and this is what I'm doing in my applicationDidBecomeActive:
CCScene *s=[director_ runningScene];
GameLayer *l=[(GameLayer*)[s getChildByTag:GAME_LAYER_TAG];
if (l!=nil) [l myUpdate];
I don't think it's relevant to the problem though because myUpdate does get called but I still have that split second glitch on my Ipad, as if it starts back from its last state no matter what.
In apples clock app, in applicationDidEnterBackground it hides the timer text, so that when the app comes into the foreground you see a blank UI for the split second where your app is loading the new timer data in the UI. Also, you may want to call some of your applicationDidBecomeActive code in applicationWillEnterForeground, which is called first. But keep in mind, applicationWillEnterForeground is not called when the app first launches.
There will always be a delay between when your app comes into the foreground, and when the UI updates. Theres no way to fix that, so you might as well use what apple uses to get around the issue.
Well I tried to hide my UI in both applicationWillResignActive and applicationDidEnterBackground. Since applicationWillResignActive is called first and before going into preview (double click on HOME) it causes a "not so pretty" preview but I thought at least it would solve my original problem. It didn't (not, on my IPad). It looks like the system takes the screen shot even before applicationWillResignActive.
I checked the timer in the official clock app and I see the clock is updating even when the app is in the background (in preview), so they "cheat" anyway...

performSelector:withObject:afterDelay: not firing as scheduled after app returns to foreground

I am scheduling a selector using performSelector:withObject:afterDelay: with a five minute delay. If the device goes to sleep for more than five minutes while this is scheduled, then one of two things happens:
1) If the device is connected to my laptop, and the app is running in the debugger, then when the app wakes back up the selector fires immediately. It seems like the "clock keeps running" for the delay.
2) If the device is not connected, then there is a delay before the selector fires. It seems like the "clock pauses" for the delayed selector.
It seems strange that there would be two different behaviors depending on whether the device is connected to the debugger or not. Any ideas?
I've noticed that the debugger seems to force the app alive in the background.
Typically, if you are debugging on a real device and press the home button, nothing special will happen, even after 10 minutes.
On the other hand, if you force kill your app on the device, you'll end up receiving a SIGKILL signal which will terminate your debug session.
The app is paused, when in background. You should generally save timers etc, in applicationDidEnterBackground and restart them in applicationDidBecomeActive.

Resources