Sample Code:
struct ContentView: View {
@State private var viewModel = ContentViewModel() // Compiler Error: "Call to main actor-isolated initializer 'init()' in a synchronous nonisolated context"
var body: some View {
Text("Hello World")
}
}
@Observable
@MainActor
class ContentViewModel {
// some properties here..
}
What is the best way to fix this issue? I think I can mark ContentView
with MainActor
, but is it a good practice and best way to solve it?