How can an iOS app do work when it's not on screen?
iOSMid-level
In shortOnly through specific mechanisms. beginBackgroundTask for finishing short work, a background URLSession for uploads and downloads, BGTaskScheduler for system-scheduled refresh and processing, and background modes for audio, location or VoIP. The system decides when scheduled work runs.
Model answer
When the scene goes to the background, the app gets a few seconds and is then suspended; later it may be terminated without notice. So first: save user data at .background.
Then pick the tool for the job:
- Finish something you started:
beginBackgroundTaskgives a short extra window (about 30 seconds, not guaranteed). Always end it, including in the expiration handler. - Transfers: a background
URLSessionwith file-based upload/download tasks. The system continues them while the app is suspended or terminated and relaunches the app when they finish. - Periodic refresh:
BGAppRefreshTaskviaBGTaskScheduler(or SwiftUI’s.backgroundTask(.appRefresh)): short, scheduled by the system based on usage, battery and network. Reschedule on every run. - Heavy maintenance:
BGProcessingTask, usually while charging and idle. - Continuous features: background modes for audio playback, navigation/location, VoIP.
The system decides: earliestBeginDate is only a minimum, force-quit apps don’t get background time, silent pushes are throttled. So refresh on launch too and keep a persistent queue of pending work.
What they're looking for
The right tool for each kind of work, the system's control over scheduling, and the habits that make it reliable (save at background, reschedule, expiration handlers, persistent queues).
Common mistakes
- Assuming a timer or a Task keeps running after the app is backgrounded.
- Expecting refreshes at exact intervals.
- Relying on applicationWillTerminate to save.