concurrency runtime error #1616
-
|
I'm getting the following error when trying to use the .task modifier on a view within a WithViewStore I don't fully understand what I'm doing wrong since I've seen it used here: https://www.pointfree.co/collections/composable-architecture/async-composable-architecture/ep199-async-composable-architecture-effect-lifetimes It looks somewhat like this in my view: struct ToolbarItems: ToolbarContent {
let store: StoreOf<Feature>
init(store: StoreOf<Feature>) {
self.store = store
}
var body: some ToolbarContent {
ToolbarItemGroup(placement: .primaryAction) {
WithViewStore(store, observe: ViewState.init) { viewStore in
Menu {
Button {
viewStore.send(.buttonTapped)
} label : {
Text("buton text")
}
} label: {
Text("menu")
}
.task { await viewStore.send(.onAppear).finish() }
}
}
}
}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
For quite intricate reasons, .task { @MainActor in await viewStore.send(.onAppear).finish() } |
Beta Was this translation helpful? Give feedback.
For quite intricate reasons,
View(often) inherits aMainActorcontext that is inferred in.taskmodifiers, especially when directly used in.body. It seems it's not the case forToolbarContent. You can probably work around by adding@MainActorat the beginning of your.task: