Tag: extension

Background tasks in iOS

lot's of gears

As already discussed in Background task in iOS action extension, it sometimes becomes necessary to perform time consuming tasks in the background. This is really important, if such a task would block the user interaction. Especially for action and share extensions, this might lead to some waiting time before a task completes and the extension disappears. After several attempts to realise a working solution, the following code help to extends an App’s background execution time.

This code was described in the article Extending Your App’s Background Execution Time.

Extending the background execution time in an app

func performTask()
{
   // Perform the task on a background queue.
   DispatchQueue.global().async {
      // Request the task assertion and save the ID.
      self.backgroundTaskID = UIApplication.shared.
                 beginBackgroundTask (withName: "Perform Long Task") {
         // End the task if time expires.
         UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)
         self.backgroundTaskID = UIBackgroundTaskInvalid
      }
            
      // Run task synchronously.
      self.performLongTask()
            
      // End the task assertion.
      UIApplication.shared.endBackgroundTask(self.backgroundTaskID!)
      self.backgroundTaskID = UIBackgroundTaskInvalid
   }
}

Extending the background execution time in an extension

func performTask()
{
   // Perform the task in background.
   let processinfo = ProcessInfo()
   processinfo.performExpiringActivity(withReason: "Long task") { (expired) in
      if (!expired) {
         // Run task synchronously.
         self.performLongTask()
      }
      else {
         // Cancel task.
         self.cancelLongTask()
      }
   }
}

As mentioned in the documentation, the ProcessInfo code block is called a second time if the system need to suspend the process:

If your block is still executing and the system need to suspend the process, the system executes your block a second time with the expired parameter set to true. Your block must be prepared to handle this case. When the expired parameter is true, stop any in-progress tasks as quickly as possible.

Source: https://developer.apple.com/documentation/foundation/processinfo/1617030-performexpiringactivity

Important: it’s important that the tasks are executed synchronously. When the end of the block is reached, the thread will terminate and end the execution of your task. If your tasks are asynchron, you can use a loop like shown below to wait inside the background thread until the asynchron task is finished:

// Keep background thread alive until asynchron task ends.
repeat {
    sleep(1)
} while(taskIsRunning)

General thoughts on using background tasks in iOS

A good summary of background tasks is available at https://forums.developer.apple.com/thread/85066


Photo by Jonathan Borba on Unsplash.