Loading...
Loading...
Writes and reviews Swift code for background execution on Apple platforms - BGTaskScheduler (app refresh, processing, continued processing), beginBackgroundTask assertions, background URLSession downloads/uploads, silent and VoIP push, background modes (audio, location, BLE), the SwiftUI .backgroundTask modifier, and macOS schedulers. Use when scheduling deferred work, finishing work after the app backgrounds, transferring files in the background, waking the app via push, or debugging background tasks that never run.
npx skill4agent add n0an/background-execution-agent-skill background-executionreferences/fundamentals.mdBGTaskSchedulersetTaskCompletedreferences/bg-task-scheduler.mdbeginBackgroundTaskendBackgroundTaskperformExpiringActivityreferences/task-assertions.mdURLSessionreferences/background-url-session.mdreferences/background-push.mdUIBackgroundModesreferences/background-modes.mdreferences/background-audio.mdScenePhase.backgroundTaskreferences/swiftui-background-tasks.mdNSBackgroundActivitySchedulerbeginActivitySMAppServicereferences/macos-background.mdreferences/testing-and-debugging.mdreferences/anti-patterns.mdreferences/bg-task-scheduler.mdBGAppRefreshTaskreferences/testing-and-debugging.mdbackgroundRefreshStatusreferences/bg-task-scheduler.mdBGProcessingTaskreferences/testing-and-debugging.mdreferences/bg-task-scheduler.mdBGContinuedProcessingTaskreferences/fundamentals.mdreferences/task-assertions.mdbeginBackgroundTaskendBackgroundTaskperformExpiringActivityreferences/background-url-session.mdreferences/testing-and-debugging.mdreferences/background-push.mdreferences/background-modes.mdremote-notificationreferences/background-audio.mdaudioreferences/background-audio.md.playAndRecordreferences/background-modes.mdlocationreferences/swiftui-background-tasks.mdScenePhase.backgroundTask(.appRefresh/.urlSession)references/bg-task-scheduler.mdBGTaskSchedulerreferences/macos-background.mdNSBackgroundActivitySchedulerbeginActivitySMAppServicereferences/testing-and-debugging.mdreferences/anti-patterns.mdreferences/anti-patterns.mdDoes the work require user consent or input mid-flight?
-> Background runtime is wrong. Bring it to the foreground.
Is work already running and you just need to finish it as the app backgrounds?
-> beginBackgroundTask / endBackgroundTask (apps)
-> ProcessInfo.performExpiringActivity (app extensions, watchOS)
Is it a user-initiated long task that should continue with visible progress? (iOS/iPadOS 26)
-> BGContinuedProcessingTask
Is it a large/long file download or upload that must survive suspension/termination?
-> background URLSession
Does the server need to tell the app new content exists?
-> background (silent) push (apns-push-type: background, content-available: 1)
Is it an incoming call invitation?
-> PushKit + CallKit (report the call on every push)
Is it periodic content refresh before the user opens the app?
-> BGAppRefreshTask
Is it long, deferrable maintenance / ML / cleanup?
-> BGProcessingTask (optionally requiresExternalPower / requiresNetworkConnectivity)
Does the app need to keep playing audio or recording the mic (incl. while locked)?
-> audio UIBackgroundMode + active AVAudioSession (.playback or .playAndRecord) [background-audio.md]
Does the app need to keep running for location / BLE / VoIP?
-> declare the matching UIBackgroundModes capability [background-modes.md]
macOS, deferrable repeating maintenance?
-> NSBackgroundActivityScheduler
macOS, keep critical non-UI work from being napped?
-> ProcessInfo.beginActivity / performActivityShort content refresh, system picks the time around app usage?
-> BGAppRefreshTask (UIBackgroundModes: fetch)
Long maintenance, ideally on charger + network?
-> BGProcessingTask (UIBackgroundModes: processing; requiresExternalPower / requiresNetworkConnectivity)
User-tapped long work with progress UI, continues after backgrounding? (iOS 26)
-> BGContinuedProcessingTask (title/subtitle, progress, submission strategy, optional .gpu)
Long processing of health-research-study data? (iOS 17)
-> BGHealthResearchTaskBGTaskSchedulerApp.init()application(_:didFinishLaunchingWithOptions:)BGContinuedProcessingTaskBGTasksetTaskCompleted(success:)BGTaskSchedulerrequiresExternalPowerrequiresNetworkConnectivityBGProcessingTaskRequestearliestBeginDatebeginBackgroundTaskendBackgroundTask.invalidbeginBackgroundTaskbackgroundTimeRemainingDBL_MAXbeginBackgroundTaskperformExpiringActivitydataTaskURLSessionURLSessionhandleEventsForBackgroundURLSessionurlSessionDidFinishEvents(forBackgroundURLSession:)didFinishDownloadingTolocationuploadTask(with:fromFile:)apns-push-type: backgroundapns-priority: 5apscontent-availableallowsBackgroundLocationUpdates = truelocationlocationaudioAVAudioSession.playback.playAndRecord.record.notifyOthersOnDeactivationinterruptionNotification.shouldResumerouteChangeNotification.oldDeviceUnavailableAVAudioApplication.requestRecordPermission()NSMicrophoneUsageDescriptionAVAudioEngineAVAudioFileUIBackgroundModesBGTaskSchedulerURLSessionbackgroundRefreshStatus.denied.restricted.onAppear.tasksceneDidEnterBackground(_:)applicationDidEnterBackground(_:)NSBackgroundActivitySchedulerProcessInfo.beginActivityendActivityperformActivityperformExpiringActivity_simulateLaunchForTaskWithIdentifier:_simulateExpirationForTaskWithIdentifier:BGTaskScheduler.onAppear// Before
.onAppear {
BGTaskScheduler.shared.register(forTaskWithIdentifier: id, using: nil) { ... }
}
// After - register in App.init() / didFinishLaunching
init() {
BGTaskScheduler.shared.register(forTaskWithIdentifier: id, using: nil) { task in
self.handleRefresh(task: task as! BGAppRefreshTask)
}
}setTaskCompleted// Before
func handleRefresh(task: BGAppRefreshTask) { refresh() }
// After
func handleRefresh(task: BGAppRefreshTask) {
scheduleNext()
let op = RefreshOperation()
task.expirationHandler = { op.cancel() }
op.completionBlock = { task.setTaskCompleted(success: !op.isCancelled) }
queue.addOperation(op)
}App.init()setTaskCompletedreferences/fundamentals.mdreferences/bg-task-scheduler.mdBackgroundTasksBGAppRefreshTaskBGProcessingTaskBGContinuedProcessingTaskBGHealthResearchTaskreferences/task-assertions.mdbeginBackgroundTaskendBackgroundTaskbackgroundTimeRemainingProcessInfo.performExpiringActivityBGTaskSchedulerreferences/background-url-session.mdURLSessionhandleEventsForBackgroundURLSessionreferences/background-push.mdreferences/background-modes.mdUIBackgroundModesreferences/background-audio.mdAVAudioSessionAVAudioEnginereferences/swiftui-background-tasks.mdScenePhase.backgroundTaskreferences/macos-background.mdNSBackgroundActivitySchedulerProcessInfo.beginActivitySMAppServicereferences/testing-and-debugging.mdbackgroundRefreshStatusreferences/anti-patterns.md