Completion handler blocks not supported in background sessions?

let session_config = URLSessionConfiguration.background(withIdentifier: "cxxczuifgdgd")

let session = URLSession(configuration: session_config)

let task=session.downloadTask(with: request) { (url, response, error) in

print("anusha")

}

task.resume()

created a url session for background downloads as above...but app is getting crashed with error...


libc++abi.dylib: terminating with uncaught exception of type NSException

(lldb)

Completion handler blocks not supported in background sessions?

Correct. It’s impossible for a background session to support completion handler blocks because:

  • Such blocks can capture arbitrary state from your process

  • Your process can be terminated while the task is running and then relaunched to handle completion

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

This is a slide from Efficiency awaits: Background tasks in SwiftUI from WWDC 2022.

It literally uses this code, which crashes with the above error when actually used.

Can we get some retraction here?

Completion handler blocks not supported in background sessions?
 
 
Q