How to use Observation to actually observe changes to a property

This is almost what you want:

func withContinousObservation<T>(of value: @escaping @autoclosure () -> T, execute: @escaping (T) -> Void) {
    withObservationTracking {
        execute(value())
    } onChange: {
        DispatchQueue.main.async {
            withContinousObservation(of: value(), execute: execute)
        }
    }
}

Ability of cancelling is missing in this implementation.

1 Like