Completion handler blocks are not supported in background sessions

When I try to implement the new Background Task options in the same way as they show in the WWDC video (on watchOS) likes this:

let config = URLSessionConfiguration.background(withIdentifier: "SESSION_ID")
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()
}

I'm receiving the following error:

Terminating app due to uncaught exception 'NSGenericException', reason: 'Completion handler blocks are not supported in background sessions. Use a delegate instead.'

Did I forget something?

Did I forget something?

Nope.

What you’re asking for here is architecturally impossible. The typical use case for a URLSession background session runs like this:

  1. Your app starts a task.

  2. The user presses the Home button, which suspends your app. The request continues running in a system process (nsurlsessiond).

  3. After a while the system might run low on memory and it terminates your app.

  4. What the task finishes, the system relaunches your app in the background to handle the result.

You can’t use a completion handler in this scenario because it relies on state from the current process and that process is gone by the time you get to step 4.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

So the code above isn't explicitly using completionHandlers for the URL tasks

Is the expectation that await is essentially a "completionHandler" under the hood, and the problem is asynchronously interacting with background tasks from your app entirely?

You can't use async/await for BG tasks and you must only interact with a given BG task via the delegate?

Can we get a retraction for the whole Efficiency awaits: Background tasks in SwiftUI talk from WWDC 2022 please?

I believe it is completely wrong, and the author knows it.

  • it uses the following code, which crashes the app when executed, so clearly the code hasn't been ever run

  • and it leaves this section empty because there seems to be no code that could work there - the only mechanism for handling the background downloads is delegate based!

I just wasted two days trying to make the above code work, before arriving here to document this futility. Is this some kind of work in progress code, that was started for iOS 16 and never finished, while the remnants remain in the codebase?

Sorry I didn’t reply earlier. I’m not sure how I missed all the previous replies [1].

Written by tomaskafka in 823711022
Can we get a retraction for the whole [Efficiency awaits] please?

Please file a bug against that video. I’m not sure how the folks responsible will handle that, but a bug report is the best way to get your feedback to them.

Please post your bug number, just for the record.

Written by tomaskafka in 823711022
I just wasted two days trying to make the above code work

Bummer )-:


For all the other folks who posted comments on my original reply, if you have outstanding issues please reply here [2] with the current state of affairs and I’ll take a look.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

[1] Some of those are comments, and comments are easy to miss as I explain in Quinn’s Top Ten DevForums Tips. However, there was at least one reply and it’s a mystery as to why I missed that.

[2] Again, reply, don’t comment.

Completion handler blocks are not supported in background sessions
 
 
Q