Background Task in Swift

Riccardo Cipolleschi
The Startup
Published in
4 min readMay 20, 2020

--

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:

  1. It informs the OS that, once put into the background, the app needs more time to complete a long-running task.
  2. 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.

--

--

Riccardo Cipolleschi
The Startup

Hey there, I’m Riccardo. Software engineer at Meta. I have a passion for iOS and I love to share my knowledge with others.