How DispatchQueue.asyncAfter behaves when application goes to background?
How DispatchQueue.asyncAfter behaves when application goes to background?
I would like to know how DispatchQueue.asyncAfter(deadline:execute:) behaves in case of going to background. I tried to find more information in documentation, but it's nothing there.
DispatchQueue.asyncAfter(deadline:execute:)
Let's assume case in which I invoke:DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) { ... }
DispatchQueue.main.asyncAfter(deadline: .now() + 60.0) { ... }
The most interesting part to me is to know when the timer counting down 60 seconds stops and resumes.
Maybe I should use DispatchQueue.asyncAfter(wallDeadline:execute:) to be independent from background/foreground mode and timer freezing?
DispatchQueue.asyncAfter(wallDeadline:execute:)
@PrashantTukadiya of course I can test it, but in case of going background, few tests don't prove anything :) I would prefer to know what does it do under the hood at least.
– Wojciech Kulik
36 mins ago
3 Answers
3
It depends how your application configured the background modes. For example if you had background modes enabled and setup location monitor to always the can survive in background forever if user did not closed the app. So in this case the DispatchQueue.asyncAfter callback will be called.
On the other hand if background modes is disable the app will enter in freez state when is put in background so your callback will not be called. Note the timer will not stop, so if you schedule a dispatch in 60 seconds and you stay in background for 100 seconds when will come back to foreground the callback will be called immediately. If you stay in background only 30 seconds then when you will came back you will had to wait 30 more seconds.
Note you can still had background modes disabled and keep with app alive up to 3 minutes in the background if used the background task.
self.backgroundTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
// called when the app is about to enter in freez state
[[UIApplication sharedApplication] endBackgroundTask:weakSelf.backgroundTask];
}];
and what will happen if I restore the application to foreground (unfreeze)? Will it perform task immediately or after remaining time from the moment when it was frozen? The most interesting part to me of this question is when 'timer' stops?
– Wojciech Kulik
20 mins ago
In short, when the app is suspended (when the user goes to another app), all execution stops and this timer won’t fire unless you’ve configured it for background execution. See App programming Guide for iOS: Background Execution.
As an aside, be wary of testing this from Xcode because being attached to the debugger can change the app life cycle.
A timer started in the foreground will run in the background, if the app is already running in the background for some other reason (e.g. it is playing music and has the background mode for audio turned on, or it is performing background Core Location and has background mode for Core Location turned on).
Otherwise, it will pause in the background.
And, as Rob rightly said, you cannot test this from Xcode, and especially not in the Simulator, which gets background behavior wrong.
– matt
2 mins ago
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Give a try and let us know :)
– Prashant Tukadiya
37 mins ago