Skip to content

Class

SQLiteHookToken

Returned by every add…Observer and set…Validator call, whether sync or async, of any type. Call cancel() to unregister its associated callback.
final class SQLiteHookToken

Token Lifetime Behavior

The lifetime behavior depends on the SQLiteObserverLifetime parameter:

  • scoped - observer automatically cancels when token is deallocated:

    // Async usage:
    let token = try await connection.addUpdateObserver(
        lifetime: .scoped
    ) { event in
        print("Auto-canceled when token is deallocated")
    }
    // Sync usage:
    let token = connection.addUpdateObserver(lifetime: .scoped) { event in
        print("Auto-canceled when token is deallocated")
    }
    // Observer stops when `token` goes out of scope
  • pinned - observer remains active until explicitly canceled:

    // Observer stays active until connection closes or explicit cancel
    let token = try await connection.addUpdateObserver(
        lifetime: .pinned
    ) { [weak self] event in
        self?.logger.info("This will be called until explicitly canceled!")
    }
    // Call token.cancel() when you want to stop the observer

Important

Use [weak self] or [unowned self] in observers to avoid retain cycles, especially with pinned lifetime.

Note

Calling cancel() after the connection has been closed is a no-op.

Topics

Operators

Instance Properties

Instance Methods

Relationships

Conforms To

  • Swift.Equatable
  • Swift.Hashable
  • Swift.Sendable
  • Swift.SendableMetatype