One Logo Pool Ball
SwiftData · IOS API REFERENCE

HistoryObserver

class · iOS 27.0+ · SDK 27.1

final class HistoryObserver

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.

init · iOS 27.0+

init

TypeScript
static init(historyTokens: (Record<string, SDKHandle>) | null, authors: Array<string>, modelContainer: ModelContainer): HistoryObserver
Swift declaration
convenience init(historyTokens: [String : any HistoryToken]? = nil, observedModels: [any PersistentModel.Type] = [], authors: Set<String> = [], modelContainer: ModelContainer, isolation: isolated (any Actor)? = #isolation) throws

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.

HistoryObserver.observedModels

Passing a Swift type itself is not supported.

All SwiftData types →