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
}
}
}
0
0
27