Hi, folks.
I know that in the new observation, class property changes can be automatically notified to SwiftUI, which is very convenient. But in the new observation framework, how to monitor the property changes of different model classes? For example, class1 has an instance of class2, and I need to notify class1 to perform some actions and make some changes when some properties of class2 are changed. How to do it in observation? In the past, I could use combined methods to write the second part of the code for monitoring. However, using the combined framework in observation is a bit confusing. I know this method can be withObservationTracking(_:onChange:) but it needs to be registered continuously. If Observation is not possible, do I need to change my design structure?
Thanks.
// Observation
@Observable class Sample1 {
var count: Int = 0
var name = "Sample1"
}
@Observable class Sample2 {
var count: Int = 0
var name = "Sample2"
var sample1: Sample1?
init (sample1 : Sample1) {
self.sample1 = sample1
}
func render() {
withObservationTracking {
print("Accessing Sample1.count: \(sample1?.count ?? 0)")
} onChange: { [weak self] in
print("Sample1.count changed! Re-rendering Sample2.")
self?.handleSample1CountChange()
}
}
private func handleSample1CountChange() {
print("Handling count change in Sample2...")
self.count = sample1?.count ?? 0
}
}
// ObservableObject
class Sample1: ObservableObject {
@Published var count: Int = 0
var name = "Sample1"
}
class Sample2: ObservableObject {
@Published var count: Int = 0
var name = "Sample1"
var sample1: Sample1?
private var cancellables = Set<AnyCancellable>()
init (sample1 : Sample1) {
self.sample1 = sample1
setupSubscribers()
}
private func setupSubscribers() {
sample1?.$count
.receive(on: DispatchQueue.main)
.sink { [weak self] count in
guard let self = self else { return }
// Update key theory data
self.count = count
self.doSomeThing()
}
.store(in: &cancellables)
}
private func doSomeThing() {
print("Count changes, need do some thing")
}
}