I too would like more detail on this as it sounds like a great feature!
But, I can't get the code snippet shown at 11:26 in the video Efficiency awaits: Background tasks in SwiftUI to work?
Running this:
let config = URLSessionConfiguration.background(withIdentifier: "isStormy")
config.sessionSendsLaunchEvents = true
let session = URLSession(configuration: config)
let response = await withTaskCancellationHandler {
try? await session.data(for: request)
} onCancel: {
let task = session.downloadTask(with: request)
task.resume()
}
the line try? await session.data(for: request) throws the following exception:
Completion handler blocks are not supported in background sessions. Use a delegate instead.
Using Xcode 14.1 and a iOS 16.1 simulator.
Creating the session with a delegate (like let session = URLSession(configuration: config, delegate: self, delegateQueue: nil)) doesn't help, same exception.
Has anyone got this to work?
Topic:
App & System Services
SubTopic:
General
Tags: