Open
Description
View state in SwiftUI must only be changed on the main thread (nee main actor). We currently force effects onto the main thread through .receive(on: DispatchQueue.main)
. However, we do not force send
on to the main thread, relying on callers to do the right thing, and SwiftUI to complain if you don't.
It might be nice to force send
onto the main thread to make changing state off main thread impossible. We might do this through decorating Store with @MainActor
or perhaps through decorating send
with @MainActor
.
Code Sketch
public protocol StoreProtocol {
associatedtype Model: ModelProtocol
@MainActor var state: Model { get }
@MainActor func send(_ action: Model.Action) -> Void
}
@MainActor
public final class Store<Model>: ObservableObject, StoreProtocol
where Model: ModelProtocol
{
// ...
}
Tested in-app on 2023-07-07. No major problems, but a few Converting function value of type '@MainActor (NotebookAction) -> ()' to '(NotebookAction) -> Void' loses global actor 'MainActor'
. We may want to make send
nonisolated.
Activity