🚀

Background Tasks in iOS

Jun 15, 2025

Overview

This lecture explains how to use Continuous Background Tasks in iOS and iPadOS to perform long-running operations even when the app is backgrounded, including setup, execution, reporting progress, and handling completion or cancellation.

Background Task Concepts

  • Continuous Background Tasks let apps finish critical work if the user backgrounds the app before the job ends.
  • BGContinuedProcessingTask starts in the foreground and can continue in the background if needed.
  • Suitable for tasks like exporting video, processing images, or analyzing sensor data.
  • Tasks can use GPU and CPU resources, as well as network access, during background execution.
  • The system may queue tasks or reject them based on available resources and priority.

Creating and Configuring a Background Task

  • Create a BGContinuedProcessingTaskRequest with a unique identifier (using your app's bundle ID).
  • Set a title and subtitle for task identification in the system UI.
  • To enable GPU use, set requiredResources to .gpu and ensure the app has Background GPU Access entitlement.
  • Check BGTaskScheduler.supportedResources for .gpu before enabling GPU processing.

Submission Strategy

  • The default strategy queues tasks if immediate resources aren't available.
  • Set request.strategy = .fail to have submission fail if the system can't execute the task right away.

Running and Registering a Task

  • Register the task with BGTaskScheduler using the taskIdentifier and a launch handler.
  • The launch handler receives a BGContinuedProcessingTask instance to manage and execute the long-running job.

Submitting a Task Request

  • Use BGTaskScheduler.shared.submit(request) to submit the properly configured task request for execution.

Reporting Progress

  • Report accurate progress using the Progress class (conforms to ProgressReporting protocol).
  • Update task.progress.completedUnitCount regularly to reflect real job progress.
  • Users can monitor and cancel tasks via the Live Activity system UI if stuck.

Handling Task Completion and Expiry

  • Use the task.expirationHandler to handle cancellation or early termination.
  • On normal completion, update progress and return success; if expired or canceled, handle as failure.
  • Tasks can fail due to app errors, user cancellation, or system-imposed expiration.

Key Terms & Definitions

  • Continuous Background Task — A background task that continues running after the app moves to the background.
  • BGContinuedProcessingTask — The API class representing a continuous background processing task.
  • BGContinuedProcessingTaskRequest — The request object used to configure and submit a continuous background task.
  • BGTaskScheduler — The system scheduler for managing background tasks.
  • ProgressReporting — A protocol for reporting task progress to the system.
  • Expiration Handler — Code run if a task is canceled or expires before completion.

Action Items / Next Steps

  • Implement task registration, request creation, and submission in your app as needed.
  • Enable Background GPU Access in Xcode if your tasks require GPU resources.
  • Regularly report progress and handle task completion, expiration, or failure in your code.