Finite-Length Tasks in background iOS swift

Suppose if you are running a process and in between you press the home button and your app goes to the background, then your app should get some time to finish what it was doing or at least in most cases it should be able to save any important data. iOS provides a way for the app to request a little more time to complete the task. You should tell the OS that you are doing something and need a little more time.You should also handle the task expiration (things to do when the allotted time is expired or if the task is finished before this time). In this case the OS will give some more time for the app to perform the task it was doing. Normally the OS will give upto 3 minutes(180 seconds) to complete the task . This is just a general observation. The time can be greater than or less than 3 minutes.This is not given in the official documentation.

Technically, this is not a background mode at all, as you don’t have to declare that your app uses this mode in Capabilities. Instead, it’s an API that lets you run arbitrary code for a finite amount of time when your app is in the background.

As per the Apple’s documentation on Background Execution, it says:

Apps moving to the background are expected to put themselves into a quiescent state as quickly as possible so that they can be suspended by the system. If your app is in the middle of a task and needs a little extra time to complete that task, it can call the beginBackgroundTaskWithName:expirationHandler: or beginBackgroundTaskWithExpirationHandler: method of the UIApplication object to request some additional execution time. Calling either of these methods delays the suspension of your app temporarily, giving it a little extra time to finish its work. Upon completion of that work, your app must call the endBackgroundTask: method to let the system know that it is finished and can be suspended.

Apples recommended way of performing a finite length task is by assigning a background task before starting the task and to end it as soon as it finishes — regardless of whether the app ever moved to the background during the task’s execution.

Note: Always provide an expiration handler when starting a task, but if you want to know how much time your app has left to run, get the value of the backgroundTimeRemaining property of UIApplication.

registerBackgroundTask() tells iOS that you need more time to complete whatever it is you’re doing in case the app is backgrounded. After this call, if your app is backgrounded it will still get CPU time until you call endBackgroundTask().

Well, almost. If you don’t call endBackgroundTask() after a period of time in the background, iOS will call the closure defined when you called beginBackgroundTask(expirationHandler:) to give you a chance to stop executing code. So it’s a very good idea to then call endBackgroundTask() to tell the OS that you’re done. If you don’t do this and continue to execute code after this block is called, your app will be terminated!

If you enjoyed reading this post, please share and recommend it so others can find it 💚💚💚💚💚💚 !!!!

Like what you read? Give Abhimuralidharan a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.