Thank you. Again, just to clarify, why don't you want modify class A? Is it not "yours" and it would be troublesome to ask the other team changing it or is there another reason?
Given the constraints (no NSObject, etc) polling seems to be the only available option (with all its drawbacks: CPU toll, possibility to miss quickly changing values, ...). (crossing that out as I forgot about Observable).
In real life classA is used as view model for SwiftUI view
And I would like to observe value of x in the test cases.
If possible I want to avoid adding code just to class A.
Certainly SwiftUI has a mechanism of observing changes and they have a function called withObservationTracking which looks promising but I can't get it to work.
Given below is my failed attempt:
import Foundation
import Observation
@Observable
class A {
var x = 0
}
class Testing {
let source = A()
func observe() {
print("observe started")
withObservationTracking {
_ = source.x
} onChange: { [oldValue = source.x] in
print("x oldValue: \(oldValue)")
}
print("observe ended")
}
func change() async {
print("change started")
try? await Task.sleep(for: .seconds(1))
source.x = 20
try? await Task.sleep(for: .seconds(1))
source.x = 30
try? await Task.sleep(for: .seconds(1))
source.x = 40
print("change ended")
}
}
let testing = Testing()
Task {
testing.observe()
}
await testing.change()
Output:
change started
observe started
observe ended
x oldValue: 0
change ended
Program ended with exit code: 0