Skip to content

Instance Method

withUpdateObserver(_:body:)

Execute a block with a temporary update observer.
func withUpdateObserver<T>(_ callback: @escaping SQLiteConnection.SQLiteUpdateHookCallback, body: () async throws -> T) async throws -> T

Parameters

callback

The observer callback to register temporarily.

body

The block to execute with the observer active.

Return Value

The return value of the body block.

Discussion

The observer is automatically removed when the block completes, making this ideal for testing or temporary observation scenarios. The observer is removed regardless of whether the body throws.

try await connection.withUpdateObserver({ event in
    print("Update: \(event)")
}) {
    // All database operations in this block will trigger the observer
    _ = try await connection.query("INSERT INTO users(name) VALUES('Alice')", [])
    _ = try await connection.query("UPDATE users SET name='Bob' WHERE id=1", [])
    // Observer is automatically cleaned up when block exits
}

Throws

Any error thrown by the body block.