Background Task in Swift
Hi everybody, I’m Riccardo. Senior iOS Engineer at Bending Spoons, I breathe iOS development, both apps and tools and I love to share my knowledge with others
Last week, a question was running through our (virtual) office: how much time does iOS leave to the app to complete a background task?
As often happens in the technology world, the documentation is not that precise. Also, there is a lot of misinformation that makes it hard to find the right answer: someone says 30" others 3'. So… I decided to run a small experiment to find out and here it is what I did and what I discovered.
Technology
The technology chosen to run this test is the old UIApplication.beginBackgroundTask(withName:expirationHandler:)
. This method does two things:
- It informs the OS that, once put into the background, the app needs more time to complete a long-running task.
- It returns an identifier that can be used to inform the OS that the task is completed (by invoking the
UIApplication.endBackgroundTask(_:)
method).
If we forget about calling the beginBackgroundTask
method, the OS will assign something like 5" to complete the ongoing tasks and that could not be enough.