A slightly more complex code snippet works:
let session = URLSession.shared
let config = URLSessionConfiguration.background(withIdentifier: "SESSION_ID")
config.sessionSendsLaunchEvents = true
let backgroundSession = URLSession(configuration: config)
let response = await withTaskCancellationHandler {
try? await session.data(for: request)
} onCancel: {
let task = backgroundSession.downloadTask(with: request))
task.resume()
}
Topic:
App & System Services
SubTopic:
Networking
Tags: