Monitors a model container's data stores for remote changes and notifies
when new history transactions are available.
`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`` property.
Use `HistoryObserver` as an `@Observable` object and react to changes in
``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
)
```
Members (4)
get · iOS 27.0+
eventCounter
TypeScript
get eventCounter(): number
Swift declaration
final var eventCounter: Int { get }
A counter that increments each time the observer detects relevant changes.
The observer increments this value when it processes a remote change
notification that contains transactions matching its criteria. You can
observe this property from a SwiftUI view to trigger a UI update.
Creates a history observer that reports changes through its observable
``eventCounter`` property.
Use this initializer when you want to observe history changes via
SwiftUI's observation system or by reading ``eventCounter`` directly.
- Parameters:
- historyTokens: The initial history tokens keyed by store identifier.
When `nil`, the observer starts with an empty token set and captures
tokens from the first notification for each store.
- observedModels: The model types to filter for. When empty (the
default), the observer responds to changes for any model.
- authors: The transaction authors to filter for. When empty (the
default), the observer responds to changes from any author.
- modelContainer: The model container to observe.
- Throws: An error if the observer fails to fetch the initial history tokens.
get · iOS 27.0+
modelContainer
TypeScript
get modelContainer(): ModelContainer
Swift declaration
final let modelContainer: ModelContainer
The model container whose data stores this observer monitors for changes.
get · iOS 27.0+
authors
TypeScript
get authors(): Array<string>
Swift declaration
final let authors: Set<String>
The transaction authors that the observer filters for when evaluating
history transactions.
When non-empty, the observer only reports changes whose transactions
were written by one of the specified authors. When empty, the observer
treats transactions from any author as relevant.
Skipped declarations
1 declarations were excluded from these bindings. Each reason comes from the generator or the iOS SDK compiler.