Can't understand why this compiles
extension HomeReducer {
@MainActor
@Observable
final class State: BaseState {
var isPresentingSubscription = false
}
}
class BaseState {}
but this gives an error
Call to main actor-isolated initializer 'init()' in a synchronous nonisolated context
extension HomeReducer {
@MainActor
@Observable
final class State {
var isPresentingSubscription = false
}
}
extension DependencyValues {
var homeView: @Sendable () -> HomeView {
HomeKey().value
}
private struct HomeKey: DependencyKey {
typealias Value = @Sendable () -> HomeView
let liveValue: Value = {
HomeView(store: Store(initialState: HomeReducer.State(), reducer: HomeReducer()))
}
let testValue: Value = {
fatalError("testValue not implemented")
}
var previewValue: Value = {
fatalError("previewValue not implemented")
}
}
}
Sure thing! Here's example that doesn't depend on other types.
This compiles:
@MainActor
@Observable
final class State: BaseState {
var isPresentingSubscription = false
}
class BaseState {}
private struct HomeKey {
typealias Value = @Sendable () -> State
let liveValue: Value = {
State()
}
let testValue: Value = {
fatalError("testValue not implemented")
}
var previewValue: Value = {
fatalError("previewValue not implemented")
}
}
This doesn't:
@MainActor
@Observable
final class State {
var isPresentingSubscription = false
}
class BaseState {}
private struct HomeKey {
typealias Value = @Sendable () -> State
let liveValue: Value = {
State()
}
let testValue: Value = {
fatalError("testValue not implemented")
}
var previewValue: Value = {
fatalError("previewValue not implemented")
}
}
It shows error in initialisation of State
object that says
Call to main actor-isolated initializer 'init()' in a synchronous nonisolated context