You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using @State at the app level causes the object to be init too soon so should be avoided. E.g. it'll be init during Previews even when App's body isnt called.
Better fix is just to conform your class also to ObservableObject then you can continue to use it with @StateObject. So think of @Observable as an alternative just for @Published.
The text was updated successfully, but these errors were encountered:
Yes you're right, e.g. for a top level model object, you can do a fileprivate letoutside of the app struct. In Swift globals are lazy and if you use it from body then that gives the same semantics as StateObject without needing ObservableObject for your @Observableclass, e.g.
fileprivate let model = Model() // @Observable class
struct App: App {
var body: some Scene
...
.environment(model) // model is init here once.
}
}
Using
@State
at the app level causes the object to be init too soon so should be avoided. E.g. it'll be init during Previews even when App's body isnt called.Better fix is just to conform your class also to
ObservableObject
then you can continue to use it with@StateObject
. So think of@Observable
as an alternative just for@Published
.The text was updated successfully, but these errors were encountered: