DevPrep

All topics › IDEs & SDK

What can an app do in the background: BGTaskScheduler, background URLSession and their limits?

Senior PRO

The gist

A few seconds after going to the background the app is suspended: no code runs, and under memory pressure the process is silently evicted. Background work is only possible through sanctioned mechanisms, and each of them is not a promise but a request to the system: iOS decides when and for how long based on battery, network, how often the app is used and the user's settings.

Pick the tool by the job: finish what you started — beginBackgroundTask; refresh data — BGAppRefreshTask; heavy maintenance — BGProcessingTask; transfer files — background URLSession; react to a server event — silent push; run continuously — only the special background modes.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store