background-tasks
Background Tasks
Apple's BackgroundTasks framework for scheduling and running work while your app is suspended or backgrounded.
Choose the right task type
| Goal | Task class | Time limit | Trigger |
|---|---|---|---|
| Short content refresh | BGAppRefreshTask |
~30 seconds | System-decided |
| Heavy processing (ML, DB, sync) | BGProcessingTask |
Minutes | System-decided, device idle |
| Foreground job that must survive backgrounding | BGContinuedProcessingTask |
Unlimited | User action only |
| Health research data processing | BGHealthResearchTask |
Minutes | System-decided, device idle |
Limits: max 1 refresh task + 10 processing tasks schedulable at any time.
Decision tree
-
Setting up for the first time? →
references/setup.md(Info.plist keys, UIBackgroundModes, registration) -
Refreshing content periodically? →
references/app-refresh.md(BGAppRefreshTask, reschedule pattern) -
Running heavy background work? →
references/processing.md(BGProcessingTask, power/network requirements) -
Foreground task that must survive backgrounding? →
references/continued-processing.md(BGContinuedProcessingTask, GPU, progress, Live Activity) -
Managing task lifecycle (cancel, limits, pending tasks)? →
references/scheduling.md -
Handling submit errors? →
references/error-handling.md(notPermitted, tooManyPendingTaskRequests, unavailable) -
Testing during development? →
references/testing.md(LLDB debugger commands, device-only limitation)
Key rules
- Register all launch handlers before
applicationDidFinishLaunchingreturns. - Always set
expirationHandler— missing it causes the system to silently mark the task failed. - Always call
setTaskCompleted(success:)— missing it may cause the system to kill your app. - Reschedule inside the launch handler, not after it returns.
BGContinuedProcessingTaskmust be submitted as a result of a user action; it starts in the foreground.
Reference files
references/setup.md— Info.plist, UIBackgroundModes, registrationreferences/app-refresh.md— BGAppRefreshTask patternreferences/processing.md— BGProcessingTask patternreferences/continued-processing.md— BGContinuedProcessingTask, GPU, progress reportingreferences/scheduling.md— earliestBeginDate, cancel, limits, pending task inspectionreferences/error-handling.md— BGTaskScheduler.Error codes and causesreferences/testing.md— LLDB simulation commandsreferences/_index.md— Full navigation indexbackgroundtasks.md— Full Apple API reference (raw docs)
More from nonameplum/agent-skills
corebluetooth
Apple Core Bluetooth framework for BLE and Bluetooth Classic. Use for central/peripheral workflows, scanning, connecting, advertising, GATT services/characteristics, read/write/notify, L2CAP, background processing or state restoration, and error handling across Apple platforms.
42swift-package-manager
Swift Package Manager documentation - create packages, manage dependencies, build and test Swift code
32swift-health-kit
Apple HealthKit framework for health and fitness data. Use for reading/writing health samples, workout data, authorization flows, observer queries, background delivery, clinical records, activity rings, and integrating with the Health app across iPhone, Apple Watch, iPad, and visionOS.
30swift-composable-architecture
The Composable Architecture (TCA) - A library for building Swift applications with state management, composition, and testability
29swift-dependencies
A dependency management library for Swift with controlled, testable dependencies
26swift-docc
Official Swift DocC documentation markup and syntax reference
23