Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Background tasks on IOS 13 (BGTaskScheduler)

Tags:

ios

swift

xcode11

I use BGTaskScheduler for a background task as described here: https://developer.apple.com/documentation/backgroundtasks/bgtaskscheduler

My question is: should this scheduled task also work after a device restart or after I manually kill the app? If not, are there another alternatives for this?

like image 373
Sorin Lica Avatar asked May 21 '20 05:05

Sorin Lica


People also ask

What is backgroundtasks in iOS 13?

BackgroundTasks is a new framework introduced in iOS 13 to offer developers more ways to execute background work. Background task offers two type of classes: BGAppRefreshTask: To refresh your app in the background periodically and always have up to date content.

How does bgtaskscheduler work in the background?

When BGTaskScheduler sees that the system meets all the conditions required for a specific task, it will wake up your app in the background, and it will handle it the task from the scheduler. If we get a BGAppRefreshTask, we can fetch content, process it, and update our UI.

How do I run tasks in the background of my App?

Request the system to launch your app in the background to run tasks. Use the BackgroundTasks framework to keep your app content up to date and run tasks requiring minutes to complete while your app is in the background. Longer tasks can optionally require a powered device and network connectivity.

How do I use the backgroundtasks framework?

Use the BackgroundTasks framework to keep your app content up to date and run tasks requiring minutes to complete while your app is in the background. Longer tasks can optionally require a powered device and network connectivity. Register launch handlers for tasks when the app launches and schedule them as required.


3 Answers

No , After restarting the device or after killing the app manually , no Background Task will be executed or start again automatically.

It is because then the State of Your App will be Changed

Various methods of AppDelegate are given in the Apple Docs , which handles different States of the App (ForeGround/BackGround/Terminated etc.)

  1. If you manually kill your app then applicationWillTerminate(_ application: UIApplication) will be executed in your AppDelegate.swift file (So , you can set some action to perform which will be executed just before app will be manually killed.)

Note that when it executes Your any BackGround Task will also be terminated & the State of your app is changed from BackGround -> Terminated

  1. When we Switch off or Restart the device , It is an external event and has nothing to do with your app, So we are not able to determine the State of the App

Even if your App is in the Back-Ground and performing any BGTask , if device will be switching off , App won't even execute applicationWillTerminate or any AppDelegate method

like image 51
Nayan Dave Avatar answered Oct 13 '22 22:10

Nayan Dave


The answer is NO, and I don't know an alternative.
I just tested it:
I started a background task after 1 min, and in the background task handler, I set a flag in the User Defaults. I then killed the app manually.
After some minutes, I opened the app and read out the User Defaults, and the flag was not set.
This indicates to me that background tasks can only be launched into background, if they have not been killed, and the same surely applies, if the device has been restarted.

like image 29
Reinhard Männer Avatar answered Oct 13 '22 21:10

Reinhard Männer


As Reinhard and Nayan said straight answer to question is NO.

Also note: even if your app is not terminated the task will be executed only when the system decides to run it. Execution time can’t be guaranteed. Refer Advances in App Background Execution

Solution: One of the ways you can handle background execution is through handling notification in background.

Apple quote from Local and Remote Notification Programming Guide

When a background update notification is delivered to the user’s device, iOS wakes up your app in the background and gives it up to 30 seconds to run. In iOS, the system delivers background update notifications by calling the application:didReceiveRemoteNotification:fetchCompletionHandler: method of your app delegate. Use that method to initiate any download operations needed to fetch new data. Processing remote notifications in the background requires that you add the appropriate background modes to your app.

like image 22
aman.sood Avatar answered Oct 13 '22 20:10

aman.sood