I have a following scenario.
I have AppState which consists of an object of type Foo. Foo has a counter variable and I want to call objectWillChange when counter value updates so I can update the UI.
At present nothing happens. The increment function gets called but the UI never gets updated.
import Foundation
import Combine
class Foo: ObservableObject {
@Published var counter: Int = 999
func increment() {
counter += 1 // how to get notified when counter value changes
}
}
class AppState: ObservableObject {
@Published var foo: Foo = Foo()
}
// usage in Scene Delegate as Environment Object
let appState = AppState()
// Use a UIHostingController as window root view controller.
if let windowScene = scene as? UIWindowScene {
let window = UIWindow(windowScene: windowScene)
window.rootViewController = UIHostingController(rootView: accountSummaryScreen.environmentObject(appState))
UPDATE
class Foo: ObservableObject {
@Published var counter: Int = 999 {
didSet {
objectWillChange.send()
}
}
func increment() {
counter += 1 // how to get notified when counter value changes
}
}