Skip to content

Observing Changes

Husnain Ali edited this page Sep 27, 2026 · 1 revision

React to writes with a typed change feed or a live query.

Change feed

changes(of:) yields a StorageChange for every write to a type made through this storage instance:

for await change in storage.changes(of: User.self) {
    switch change {
    case .inserted(let user), .updated(let user): show(user)
    case .deleted(let user): hide(user)
    case .cleared, .expired: reload()
    }
}

Live query

updates(of:options:) yields the current results immediately, then again after each burst of writes, coalesced into a single refetch:

.task {
    do {
        for try await users in storage.updates(of: User.self, options: FetchOptions(sort: .newestFirst)) {
            self.users = users
        }
    } catch {
        self.error = error
    }
}

For indexed types, updates(of:matching:orderedBy:options:) is the same live query with an in-store filter (see Indexed Fields).

Both streams end when the consuming task is cancelled.

Clone this wiki locally