Text of developer.apple.com › forums › thread › 825703 as Styx crawled it on Nov 19, 2026. The page may have changed since. Only its words are kept, without pictures or layout, and only the first part of a long page.

Open the live page

Passing closure as a 'sending' par… | Apple Developer Forums

Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution Programming Languages Swift iOS CFNetwork You’re now watching this thread. If you’ve opted in to email or web notifications, you’ll be notified when there’s activity. Click again to stop watching or visit your profile to manage watched threads and notifications. You’ve stopped watching this thread and will no longer receive emails or web notifications when there’s activity.

Click again to start watching. fbartolom OP Created May ’26 Replies 9 Boosts 0 Views 4.4k Participants 3 I'm keeping most information in an actor and I would like to save also a closure in it that I get from func application( _ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) Task.init{ await GeoreferenceQueue.shared.setBackgroundCompletionHandler(completionHandler) } } where GeoreferenceQueue is and actor, while the caller is a class. yet I receive error: Passing closure as a 'sending' parameter risks causing data races between code in the current task and concurrent execution of the closure and Sending task-isolated 'completionHandler' to actor-isolated instance method 'setBackgroundCompletionHandler' risks causing data races between actor-isolated and task-isolated uses Answered by DTS Engineer in 887130022 So, yeah, the compiler is right to complain about this.

That completion handler is intended to be called from the main thread. By passing it to a newly created task you allow it to be called from other contexts, which is Not Good™. There isn’t one true way of resolving this issue. The best option is gonna vary based on how you’ve set up your code. For example, the following is a very expedient option that kinda matches your existing design: @main final class AppDelegate: UIResponder, UIApplicationDelegate { … func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) { let runCompletionHandler = { @MainActor in completionHandler() } Task { await GeoreferenceQueue.shared.addCompletionHandler(runCompletionHandler) } } } actor GeoreferenceQueue { static let shared = GeoreferenceQueue() func addCompletionHandler(_ h: (() async -> Void)) { … } } It works by wrapping the completion handler sync function in an async function that’s also bound to the main actor.

This function is sendable because, regardless of where it’s called from, it’ll bounce to the main actor before called completionHandler . Given that, it’s fine to pass the wrapper to the GeoreferenceQueue actor. However, that’s just one example. If I were in your shoes I’d probably move the code that keeps track of these completion handlers to a main-actor-isolated type and then loosely couple that type to GeoreferenceQueue via some sort of notification.

Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com" Replies 9 Boosts 0 Views 4.4k Participants 3 DTS Engineer OP Apple May ’26 Recommended So, yeah, the compiler is right to complain about this. That completion handler is intended to be called from the main thread. By passing it to a newly created task you allow it to be called from other contexts, which is Not Good™.

There isn’t one true way of resolving this issue. The best option is gonna vary based on how you’ve set up your code. For example, the following is a very expedient option that kinda matches your existing design: @main final class AppDelegate: UIResponder, UIApplicationDelegate { … func application(_ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) { let runCompletionHandler = { @MainActor in completionHandler() } Task { await GeoreferenceQueue.shared.addCompletionHandler(runCompletionHandler) } } } actor GeoreferenceQueue { static let shared = GeoreferenceQueue() func addCompletionHandler(_ h: (() async -> Void)) { … } } It works by wrapping the completion handler sync function in an async function that’s also bound to the main actor.

This function is sendable because, regardless of where it’s called from, it’ll bounce to the main actor before called completionHandler . Given that, it’s fine to pass the wrapper to the GeoreferenceQueue actor. However, that’s just one example. If I were in your shoes I’d probably move the code that keeps track of these completion handlers to a main-actor-isolated type and then loosely couple that type to GeoreferenceQueue via some sort of notification.

Share and Enjoy — Quinn “The Eskimo!” @ Developer Technical Support @ Apple let myEmail = "eskimo" + "1" + "@" + "apple.com" Add comment fbartolom OP May ’26 Unfortunately using your code as of: func application( _ application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: @escaping () -> Void) { let runCompletionHandler = { @MainActor in completionHandler() } Task.init{ await GeoreferenceQueue.shared.addCompletionHandler(runCompletionHandler) } } produces: Sending 'completionHandler' risks causing data races And @MainActor in your code is blue as it were recognized, in my code it remain black.

And moreover in the actor at: func addCompletionHandler(_ h: @escaping (() async -> Void)) { backgroundCompletionHandler=h } I have: /Users/fbartolom/Documents/cocoa applications/iPuja/Classes/GeoreferenceQueue.swift:78:36 Invalid conversion from 'async' function of type '() async -> Void' to synchronous function type '() -> Void' On the assignment of h to the variable In practice previously I had an error message, now two...

Add comment Etresoft OP May ’26 Judging from your other threads, perhaps you're overusing actors and completion handlers. I've only used one actor and that was on my first Swift 6 app. I've learned a lot since then. Completion handlers are a different problem. They are largely a legacy of Apple's now-abandoned GCD architecture. The Swift concurrency approach is to use sequential, but asynchronous code instead. That new Swift approach is great.

But so many Apple APIs are still based on blocks/closures. Easily the vast majority of all the "sending" problems I've encountered have been with blocks/closures. I would love to be able to write the kind of asynchronous Swift code I see in the WWDC demos. But it seems like it's never possible with Apple APIs. So my suggestion is to re-think the actor and the completion handler. Obviously, if you have an API that requires a completion handler, you often can't get around that.

Sometimes APIs do have an asynchronous version. But if they don't, then you have to handle that code more carefully. And when I look at the delegate method in question, I see that it actually does have an async version. I think your best solution is to just use that. Pass the identifier to your GeoreferenceQueue and let it handle it however it should. I think this case, an actor might be appropriate. I was thinking about your Timer actor from the other thread.

It's better to integrate timer functionality into the class where it's needed, avoiding completion handlers whenever possible. Add comment DTS Engineer OP Apple May ’26 Unfortunately using your code … produces … Sending 'completionHandler' risks causing data races Hmmm, it didn’t when I tested it. Lemme try that again: Using Xcode 26.4, I created a new project from the iOS > App template template. As part of that, I selected Storyboard from the Interface popup.

I changed the Swift Language Version build setting to Swift 6. I added GeoreferenceQueue.swift and copied my GeoreferenceQueue to it. I added my application(_:handleEventsForBackgroundURLSession:completionHandler:) method to the AppDelegate class. It built without error. But keep in mind that my example was meant to be an expedient option. I

The rest of this page is on the live site.