Here’s the only code in my codebase that uses TaskGroup
.
// Function that processes a bunch of 'action triggers' and produces a stream of actions
func run(action: Action, find: @escaping (Model.Type) -> Model?) -> AsyncStream<Action> {
AsyncStream { continuation in
Task {
await withTaskGroup(of: Void.self) { group in
for trigger in triggers {
group.addTask {
for await result in trigger(action, find) {
if result is VoidAction { continue }
continuation.yield(result)
}
}
}
await group.waitForAll()
continuation.finish()
}
}
}
}
// Later, from an async context. Process an action and dispatch its output.
Task {
for await output in run(action: action, find: { store.find($0) }) {
try await store.dispatch(output)
}
}
I was able to solve the crash by explicitly marking both the task group’s closures as @MainActor
. But would love to get a deeper understanding of why this doesn’t work.
await withTaskGroup(of: Void.self) { @MainActor group in
for trigger in triggers {
group.addTask { @MainActor in
for await result in trigger(action, find) {
if result is VoidAction { continue }
continuation.yield(result)
}
}
}
await group.waitForAll()
continuation.finish()
}