Search code examples
iosswiftxcode11

Background tasks on IOS 13 (BGTaskScheduler)


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?


Solution

  • 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