Crash on URLSession in background task

Hi,

I try to implement the BackgroundTask handling from this WWDC2022 video: https://developer.apple.com/videos/play/wwdc2022/10142

But it always crashes with this error: *** Terminating app due to uncaught exception 'NSGenericException', reason: 'Completion handler blocks are not supported in background sessions. Use a delegate instead.'

So, is the video updated or is this a bug?

Answered by DTS Engineer in 802350022

watchOS is a bit tricky but on iOS the rules are very clear: You can’t use the ‘convenience’ methods on a URLSession background session. That includes:

  • The methods that create a task with a completion handler, for example, dataTask(with:completionHandler:)

  • The Swift async methods

The code at 9:55 of that presentation does the latter, and thus is just wrong )-: Sorry about that.

Share and Enjoy

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

What platform are you targeting?

Share and Enjoy

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

What platform are you targeting?

iOS and WatchOS

watchOS is a bit tricky but on iOS the rules are very clear: You can’t use the ‘convenience’ methods on a URLSession background session. That includes:

  • The methods that create a task with a completion handler, for example, dataTask(with:completionHandler:)

  • The Swift async methods

The code at 9:55 of that presentation does the latter, and thus is just wrong )-: Sorry about that.

Share and Enjoy

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

Crash on URLSession in background task
 
 
Q