# How to use Observation to actually observe changes to a property

**URL:** <https://forums.swift.org/t/how-to-use-observation-to-actually-observe-changes-to-a-property/67591>\
**Category:** Using Swift\
**Tags:** observation\
**Created:** [September 29, 2023, 11:03am UTC](https://forums.swift.org/t/how-to-use-observation-to-actually-observe-changes-to-a-property/67591 "2023-09-29T11:03:18Z")\
**Posts on this page:** 1\
**Showing post:** 12

<div class="post-metadata">

**Author:** ![tera](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/tera/32/38201_2.png) [@tera](https://forums.swift.org/u/tera)\
**Post date:** [September 29, 2023, 7:07pm UTC](https://forums.swift.org/t/how-to-use-observation-to-actually-observe-changes-to-a-property/67591/12 "2023-09-29T19:07:16Z")

</div>

> [@pgorzelany](#):
>
> ```swift
> let sourceOfTruth = SourceOfTruth()
> let observation = withContinousObservation(of: sourceOfTruth.data) { data in
> // do something when data changes
> }
> 
> ```

This is almost what you want:

```swift
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.

---

_[View the full topic](https://forums.swift.org/t/how-to-use-observation-to-actually-observe-changes-to-a-property/67591)._
