<!--
{
  "availability" : [
    "iOS: 17.0.0 -",
    "iPadOS: 17.0.0 -",
    "macCatalyst: 17.0.0 -",
    "macOS: 14.0.0 -",
    "tvOS: 17.0.0 -",
    "visionOS: 1.0.0 -",
    "watchOS: 10.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "Swift",
  "identifier" : "/documentation/Observation/withObservationTracking(_:onChange:)",
  "metadataVersion" : "0.1.0",
  "role" : "Function",
  "symbol" : {
    "kind" : "Function",
    "modules" : [
      "Observation"
    ],
    "preciseIdentifier" : "s:11Observation04withA8Tracking_8onChangexxyXE_yyYbcyXKtlF"
  },
  "title" : "withObservationTracking(_:onChange:)"
}
-->

# withObservationTracking(_:onChange:)

Tracks access to properties.

```
func withObservationTracking<T>(_ apply: () -> T, onChange: @autoclosure () -> @Sendable () -> Void) -> T
```

## Parameters

`apply`

A closure that contains properties to track.

`onChange`

The closure invoked when the value of a property changes.

## Return Value

The value that the `apply` closure returns if it has a return value; otherwise, there is no return value.

## Discussion

This method tracks access to any property within the `apply` closure, and
informs the caller of value changes made to participating properties by way of
the `onChange` closure. For example, the following code tracks changes to the
name of cars, but it doesn’t track changes to any other property of `Car`:

```
func render() {
    withObservationTracking {
        for car in cars {
            print(car.name)
        }
    } onChange: {
        print("Schedule renderer.")
    }
}
```

---

Copyright &copy; 2026 Apple Inc. All rights reserved. | [Terms of Use](https://www.apple.com/legal/internet-services/terms/site.html) | [Privacy Policy](https://www.apple.com/privacy/privacy-policy)