Between UIKit's beginBackgroundTask(withName:expirationHandler:) and iOS 26's BGContinuedProcessingTaskRequest, which do you use for a transcript export a podcast listener starts and then leaves, and why?
answer
- seconds versus minutes
- who started it, and who can see it
- balance every begin with an end
- progress, title, subtitle, strategy
basics
~20 sUse UIKit's beginBackgroundTask(withName:expirationHandler:) to finish a short job, seconds of work, as the app leaves the foreground. A multi-minute export the listener tapped belongs in iOS 26's BGContinuedProcessingTaskRequest, which keeps running with visible, cancellable progress.
solid answer
~50 sThey solve different lengths of work. `UIApplication.beginBackgroundTask(withName:expirationHandler:)`, in UIKit, asks for a limited amount of extra time so a short job, like finishing the transcript file write, completes as the app moves to the background. Every call returns a `UIBackgroundTaskIdentifier`, which must be balanced by `endBackgroundTask(_:)`, including inside the expiration handler, or the system kills the app. A multi-minute export the listener started fits `BGContinuedProcessingTaskRequest`, added in iOS 26 for iOS and iPadOS. It is submitted from the foreground in response to the tap, starts at once or is queued, and keeps running in the background. The system shows its `title`, `subtitle` and progress in a Live Activity the listener can cancel, and the task must report progress or risk being expired as stalled. On releases before iOS 26, fall back to extra time plus resumable work.
code
swift · 42 linesimport BackgroundTasks
import Foundation
@available(iOS 26, *) // BGContinuedProcessingTaskRequest is iOS 26 and later
enum TranscriptExport {
// Info.plist's BGTaskSchedulerPermittedIdentifiers lists
// "com.example.podcasts.transcript-export.*"
nonisolated static let prefix = "com.example.podcasts.transcript-export"
// Called from the Export button's action, while the app is in the foreground.
nonisolated static func start(episodeID: String, episodeTitle: String) async throws {
let identifier = "\(prefix).\(UUID().uuidString)" // unique per export
_ = BGTaskScheduler.shared.register(forTaskWithIdentifier: identifier, using: nil) { task in
guard let export = task as? BGContinuedProcessingTask else {
task.setTaskCompleted(success: false)
return
}
run(export, episodeID: episodeID)
}
let request = BGContinuedProcessingTaskRequest(
identifier: identifier,
title: "Exporting transcript",
subtitle: episodeTitle
)
request.strategy = .fail // surface immediateRunIneligible instead of queueing
if #available(iOS 27, *) {
try await BGTaskScheduler.shared.submitTaskRequest(request)
} else {
try BGTaskScheduler.shared.submit(request) // iOS 26: no submitTaskRequest(_:) yet
}
}
nonisolated static func run(_ task: BGContinuedProcessingTask, episodeID: String) {
let work = Task {
// App-defined: writes the transcript, advancing task.progress as it goes
// and checking Task.isCancelled; returns false if it stopped early.
let finished = await TranscriptWriter.export(episodeID: episodeID, progress: task.progress)
task.setTaskCompleted(success: finished)
}
task.expirationHandler = { work.cancel() } // includes the person cancelling it
}
}go deeper
Recall the split: UIKit extra time to finish a short job while leaving the app, continued processing for a long job the person started and can watch.
Explain the mechanics: begin and end in balance with the returned identifier, and for continued processing the tap-only submission, title and subtitle, strategy, and progress reporting.
Show the failures you would prevent: unbalanced begin calls that get the app killed, duplicate identifiers, stalled progress expired by the scheduler, and no fallback before iOS 26.
Weigh the user-facing cost: continued processing puts the job in front of the person, who can cancel it, while UIKit extra time is invisible but short. Pick per feature and keep a pre-iOS 26 path.
## Two APIs for work the person is waiting on Most background work in iOS's **Background Tasks** framework is work nobody asked for: a refresh or maintenance job the system launches when it likes. A transcript export is different. The listener tapped **Export**, then switched to another app. Two Apple APIs keep that kind of work alive, and they differ mainly in how long the work takes and whether the person can see it. ## UIKit extra time: beginBackgroundTask(withName:expirationHandler:) This is a **UIKit** API on `UIApplication`, available with a name since iOS 7. It asks the system not to suspend the app while a short, critical job finishes. Apple's own example is finishing a save before the app is suspended. - It returns a **`UIBackgroundTaskIdentifier`**, or `.invalid` if running in the background isn't possible. - **Every call must be balanced by `endBackgroundTask(_:)`** with that identifier, on success and inside the expiration handler. If any task is not ended before time runs out, the system kills the app. - The expiration handler runs synchronously on the main thread, shortly before the remaining background time reaches zero; `backgroundTimeRemaining` reports that time. - The assertion is granted asynchronously, so **call it early**, before the app leaves the foreground, not at the end of `applicationDidEnterBackground(_:)`. - It needs no Background Tasks identifier and shows nothing to the person. Its time is limited, and Apple points longer jobs, such as file transfers, to a background URLSession. ## Continued processing: BGContinuedProcessingTaskRequest Added in **iOS 26**, for iOS and iPadOS only according to its header, this request is for "a workload that the system continues processing even if a person backgrounds the app". - It is **submitted from the foreground as a result of a person's action**, such as tapping a button, never for work the app decides to do on its own. - It **starts immediately if possible**. The header says `earliestBeginDate` is "outright ignored". - Its initializer takes an **identifier, a `title` and a `subtitle`**. The system shows them in a Live Activity, where the person can watch progress and cancel the job. - The identifier uses **wildcard notation**: a prefix with at least the app's bundle ID, ending in `.*`, with each submitted identifier replacing the `*` with something unique. Registration for continued-processing tasks is exempt from the before-launch-ends rule, so the app can register just before it submits. - **`strategy`** defaults to `.queue`, which waits in line when the system is busy; queued requests are cancelled if the person removes the app from the app switcher. `.fail` makes submission fail with `immediateRunIneligible` instead. - The running **`BGContinuedProcessingTask` must report progress** through its `progress` property. The header says tasks that appear stalled may be forcibly expired. - When the person cancels from the Live Activity, the framework calls the task's `expirationHandler`. If they close the app in the app switcher, running tasks are cancelled and the app receives no indication. - **`requiredResources = .gpu`** asks for background GPU time. It requires the background GPU entitlement and a device where `BGTaskScheduler.supportedResources` contains `.gpu`. ## Side by side | | UIKit `beginBackgroundTask(withName:expirationHandler:)` | `BGContinuedProcessingTaskRequest` | |---|---|---| | Framework, release | UIKit, iOS 7 | Background Tasks, iOS 26 | | Started by | Any code path, ideally before backgrounding | A person's action in the foreground | | Length | A limited amount of extra time | Minutes, while progress continues | | Person sees | Nothing | A Live Activity with progress and cancel | | Setup | None | Permitted identifier, registration, submission | | Ends with | `endBackgroundTask(_:)` | `setTaskCompleted(success:)` | ## The transcript export, decided 1. If the export reliably takes a few seconds, wrap it in UIKit extra time: begin before the work starts, and end in both the completion path and the expiration handler. 2. If it can take minutes, submit a `BGContinuedProcessingTaskRequest` from the button's action on iOS 26 and later (with `submitTaskRequest(_:)` on iOS 27, the throwing `submit(_:)` on iOS 26), advance `task.progress` as pages are written, update the subtitle with `updateTitle(_:subtitle:)` if useful, and complete the task once. 3. On earlier releases, use extra time for what fits, save how far the export got, and resume when the app returns. 4. Large downloads are neither; they belong in a background URLSession, covered in the URLSession lesson. ## Pitfalls - Starting a continued-processing request from a refresh or processing launch: it must come from the person's action in the foreground. - Registering the same identifier twice: every export needs a fresh suffix, because a second registration of an identifier is an error. - Treating progress as decoration: a task whose progress stalls may be expired by the scheduler, and the person is more likely to cancel it. - Forgetting `endBackgroundTask(_:)` in the UIKit expiration handler: if the work has not ended by then, the app is killed when its time runs out.
- In UIKit, why call beginBackgroundTask(withName:expirationHandler:) before the app leaves the foreground rather than at the end of applicationDidEnterBackground(_:)?The assertion is requested asynchronously. Apple's documentation warns that if you call it shortly before the app is due to be suspended, the system may suspend the app before the assertion is granted, and if it can't be granted the expiration handler is called.
- With BGContinuedProcessingTaskRequest, what happens when the listener cancels the export from the Live Activity?The framework calls the task's `expirationHandler`. The app should cancel the export, clean up partial output, and call `setTaskCompleted(success: false)`. Closing the app from the app switcher is different: running tasks are cancelled with no indication to the app.
- When should a continued-processing request use the .fail strategy instead of the default .queue?When starting late is worse than not starting, for example when the listener expects the export now and the app would rather tell them to retry. With `.fail`, submission errors with `immediateRunIneligible` if the system cannot run it right away; with `.queue`, it waits for room.
- beginBackgroundTask gives the app as long as it needs once it is backgrounded
- endBackgroundTask is only needed if the expiration handler fires
- A continued-processing request can be submitted from a refresh launch to start an export later
- earliestBeginDate delays a continued-processing request like any other request
- Reporting progress on a BGContinuedProcessingTask is optional decoration
Assumes the iOS 27 SDK; BGContinuedProcessingTaskRequest exists from iOS 26 on iOS and iPadOS only, and is submitted with submit(_:) on iOS 26 because submitTaskRequest(_:) arrives in iOS 27, while UIKit's beginBackgroundTask(withName:expirationHandler:) dates from iOS 7.