Last active
April 30, 2026 14:02
-
-
Save simonnickel/3e49f95630a911d8276ac69b277a6f31 to your computer and use it in GitHub Desktop.
Swift 6 - Sending
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import SwiftUI | |
| // @MainActor // This would work, as the type now is isolated and the await does not cause a boundary change. | |
| class SomethingNonSendable { // : Sendable { //, would work, because the type can now cross boundaries. | |
| let someActor: SomeActor = SomeActor() | |
| // nonisolated(nonsending) // This will make the await to be executed on the context -> MainActor | |
| // Upcoming feature flag NonisolatedNonsendingByDefault will make this the default behavior. | |
| func get() async -> Bool { | |
| // It does not matter where the context runs. The await dispatches to the actor. | |
| await someActor.get() | |
| } | |
| } | |
| actor SomeActor { | |
| func get() async -> Bool { true } | |
| } | |
| struct ContentView: View { | |
| let somethingNonSendable = SomethingNonSendable() | |
| @State private var value: Bool = false | |
| var body: some View { | |
| Text("Hello, world!") | |
| .onAppear { | |
| Task { // @MainActor in // Does not do anything, already is on MainActor. It is not important where the task runs, suspension happens at await. | |
| // let somethingNonSendable = SomethingNonSendable() | |
| // This only makes the warning go away, because the Compiler can check, that it is not used otherwise and therefor has no race conditions. | |
| // Warning: Sending 'self.somethingNonSendable' risks causing data races | |
| value = await somethingNonSendable.get() // The await is the actual suspension point, leaving MainActor, unless the func is nonisolated(nonsending). | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment