<!--
{
  "availability" : [
    "iOS: 27.0.0 -",
    "iPadOS: 27.0.0 -",
    "macCatalyst: 27.0.0 -",
    "macOS: 27.0.0 -",
    "tvOS: 27.0.0 -",
    "visionOS: 27.0.0 -",
    "watchOS: 27.0.0 -"
  ],
  "documentType" : "symbol",
  "framework" : "SwiftData",
  "identifier" : "/documentation/SwiftData/HistoryObserver",
  "metadataVersion" : "0.1.0",
  "role" : "Class",
  "symbol" : {
    "kind" : "Class",
    "modules" : [
      "SwiftData"
    ],
    "preciseIdentifier" : "s:9SwiftData15HistoryObserverC"
  },
  "title" : "HistoryObserver"
}
-->

# HistoryObserver

Monitors a model container’s data stores for remote changes and notifies
when new history transactions are available.

```
final class HistoryObserver
```

## Overview

`HistoryObserver` automatically listens for `ModelContainer/remoteChange`
notifications and determines whether the incoming changes are relevant based
on the models you specify at initialization. When relevant changes are
detected, the observer updates its [`eventCounter`](/documentation/SwiftData/HistoryObserver/eventCounter) property.

Use `HistoryObserver` as an `@Observable` object and react to changes in
[`eventCounter`](/documentation/SwiftData/HistoryObserver/eventCounter) from a SwiftUI view or other observer.

The observer tracks its position in each data store’s transaction history
using `historyTokens`, enabling incremental processing of only new
transactions since the last check.

You can scope the observer to specific model types using the
`observedModels` parameter. When provided with a non-empty array, the
observer filters incoming transactions to only those containing changes
for the specified types (and optionally their related models). When the
array is empty (the default), the observer responds to any history change
in the container.

Example usage:

```swift
let observer = try HistoryObserver(
    observedModels: [Trip.self],
    modelContainer: container
)
```

## Topics

### Creating a history observer

[`init(historyTokens:observedModels:authors:modelContainer:isolation:)`](/documentation/SwiftData/HistoryObserver/init(historyTokens:observedModels:authors:modelContainer:isolation:))

Creates a history observer that reports changes through its observable
[`eventCounter`](/documentation/SwiftData/HistoryObserver/eventCounter) property.

### Accessing observer properties

[`eventCounter`](/documentation/SwiftData/HistoryObserver/eventCounter)

A counter that increments each time the observer detects relevant changes.

[`modelContainer`](/documentation/SwiftData/HistoryObserver/modelContainer)

The model container whose data stores this observer monitors for changes.

[`observedModels`](/documentation/SwiftData/HistoryObserver/observedModels)

The model types that the observer filters for when evaluating history
transactions.

[`authors`](/documentation/SwiftData/HistoryObserver/authors)

The transaction authors that the observer filters for when evaluating
history transactions.

## Relationships

### Conforms To

[`Sendable`](/documentation/Swift/Sendable)

[`Escapable`](/documentation/Swift/Escapable)

[`Observable`](/documentation/Observation/Observable)

[`Copyable`](/documentation/Swift/Copyable)

[`SendableMetatype`](/documentation/Swift/SendableMetatype)

---

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)