DatabaseSnapshot

public class DatabaseSnapshot : DatabaseReader

A DatabaseSnapshot sees an unchanging database content, as it existed at the moment it was created.

See DatabasePool.makeSnapshot()

For more information, read about “snapshot isolation” at https://sqlite.org/isolation.html

Interrupting Database Operations

Reading from Database

  • Synchronously executes a read-only block that takes a database connection, and returns its result.

    let players = try snapshot.read { db in
        try Player.fetchAll(...)
    }
    

    Throws

    The error thrown by the block.

    Declaration

    Swift

    public func read<T>(_ block: (Database) throws -> T) rethrows -> T

    Parameters

    block

    A block that accesses the database.

  • Asynchronously executes a read-only block in a protected dispatch queue.

    let players = try snapshot.asyncRead { dbResult in
        do {
            let db = try dbResult.get()
            let count = try Player.fetchCount(db)
        } catch {
            // Handle error
        }
    }
    

    Declaration

    Swift

    public func asyncRead(_ block: @escaping (Result<Database, Error>) -> Void)

    Parameters

    block

    A block that accesses the database.