The following code (simplified for brevity) causes a crash:
Crashed: com.apple.root.user-initiated-qos.cooperative
@Published var thing: Thing?
func start() {
Task { @MainActor in await getThing() }
}
func getThing() async {
self.thing = await foo.fetchThing()
}
From my understanding, assigning self.thing
is done safely since it’s caller is assigned to the main actor (and thereby inherited). What is wrong here?
5
Unlike GCD, in Swift concurrency, the actor isolation of getThing
is dictated by how getThing
was declared, not by the actor isolation from where it was called. The fact that you called it from a task isolated to the main actor is immaterial. If you want getThing
to run on the main actor, you have a few choices:
-
Isolate
getThing
to the main actor:class Bar: ObservableObject { @Published var thing: Thing? private let foo = Foo() func start() { Task { await getThing() } } @MainActor func getThing() async { self.thing = await foo.fetchThing() } }
This pattern is a little brittle, because the burden rests on your shoulders to ensure that any methods that mutate
thing
are explicitly isolated to the main actor. You could minimize that sort of thing by isolatingthing
to the main actor, too:class Baz: ObservableObject { @MainActor @Published var thing: Thing? private let foo = Foo() func start() { Task { await getThing() } } @MainActor func getThing() async { thing = await foo.fetchThing() } }
-
Alternatively, you would isolate the whole class to the main actor which would effectively isolate
thing
andgetThing
to the main actor for you:@MainActor class Bar: ObservableObject { @Published var thing: Thing? private let foo = Foo() func start() { Task { await getThing() } } func getThing() async { self.thing = await foo.fetchThing() } }
This latter pattern is recommended in the conclusion of the Discover concurrency in SwiftUI video (and more details are provided earlier in that video).
-
As an aside, depending upon your minimum OS version, you might consider using the newer Observation framework:
import Observation @Observable @MainActor class Baz { var thing: Thing? private let foo = Foo() func start() { Task { await getThing() } } func getThing() async { thing = await foo.fetchThing() } }
Note, the Observation framework eliminates the SwiftUI problem with background updates from another actor, but you need to still solve the thread-safety issues within this type, so a global actor (such as the main actor) is still prudent.
For more information on the Observation framework, see Discover Observation in SwiftUI.
2