According to documentation, the URLSession background tasks continue even when the app is suspended. What is the lifespan of the URLSessionDownloadDelegate object when app is suspended or terminated?
Will it get re-created and re-initialize properties when the app re-launches, or will it somehow restore the existing property values?
Also, urlSessionDidFinishEvents not getting called, and what do we need to do there with the backgroundCompletionHandler? Any insights are much appreciated. We are getting ready to launch and this is a roadblock. (visionOS26.4) Thank you.
@Observable
class DownloadManager: NSObject, URLSessionDownloadDelegate {
...
let config = URLSessionConfiguration.background(withIdentifier: "TestDL")
config.sessionSendsLaunchEvents = true
var urlSession = URLSession(configuration: config, delegate: self, delegateQueue: nil)
func downloadFiles(...
{
// initiate multiple file downloads concurrently
for url in urlList {
let task = urlSession.downloadTask(with: url)
task.resume()
}
}
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
...
func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
...
func urlSession(_: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
...
// Not getting called ??
// Is this only called when app is suspended/terminated?
func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) {
print("didFinishEvents")
Task { @MainActor in
//urlSession?.finishTasksAndInvalidate()
//urlSession = nil
// not sure what to do here:
if let appDelegate = UIApplication.shared.delegate as? AppDelegate,
let completionHandler = appDelegate.backgroundCompletionHandler {
completionHandler()
appDelegate.backgroundCompletionHandler = nil
}
}
}
The below applies to iOS and all its child platforms, including visionOS. On macOS, as is so often the case, the store is subtly different (-:
What is the lifespan of the URLSessionDownloadDelegate object when app is suspended or terminated?
Once an app is suspended, one of two things can happen:
- It get resumed.
- It gets terminated.
In the first case, the app’s memory is preserved and thus any URLSession objects you created continue to exist. And the URLSession retains its delegate [1], so your delegate objects continue to exist.
If the app gets terminated then all of its memory goes away without any app code ever running again. The next time the app is launched — which might due to user activity or due to the system relaunching it in the background, for example, because your downloads have completed — it has to re-create any background sessions it was using. As part of this it has to create new delegate objects.
I generally recommend that you use a very small number of background sessions, in many apps it’s just one, and immediately create a URLSession object for each background session on app launch.
I recommend you have a read of Downloading files from websites. It covers the basics pretty well. I also have a forums post, Testing Background Session Code, that you’ll find useful. And Networking Resources has links to a bunch of other stuff.
Share and Enjoy
—
Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"
[1] Until its invalidated.