Quick question about EnvironmentObject. Should I be able to observe changes in properties of it?
For example, in the below, if I click the Toggle button, I don't appear to get notificed that isOn is updated, and my UI does not updated.
struct DataSyncView : View {
@EnvironmentObject private var foo : Foo
var body : some View {
VStack {
Text("Value : \(String(foo.isOn))")
Button("Toggle") {
foo.update()
}
}
}
class Foo : ObservableObject {
@Published var isOn = false
func update() {
foo.toggle()
}
}
Update: Got it working. Solution was to use @Observable protocol
struct DataSyncView : View {
@EnvironmentObject private var foo : Foo
var body : some View {
VStack {
Text("Value : \(String(foo.isOn))")
Button("Toggle") {
foo.update()
}
}
}
@Observable
class Foo {
var isOn = false
func update() {
isOn.toggle()
}
}