DatabaseQueue

public final class DatabaseQueue

A queue providing serialized execution of work items on a database.

A database queue manages the execution of database operations to ensure they occur one at a time in FIFO order. This provides thread-safe database access.

Database operations may be submitted for synchronous or asynchronous execution.

The interface is similar to DispatchQueue and a dispatch queue is used internally for work item management.

let dbQueue = try DatabaseQueue()
try dbQueue.sync { db in
    // Do something with `db`
}

A database queue also supports transactions and savepoints:

dbQueue.transaction { db in
    // All database operations here are contained within a transaction
    return .commit
}
  • The dispatch queue used to serialize access to the underlying database connection

    Declaration

    Swift

    public let queue: DispatchQueue
  • Creates a database queue for serialized access to an in-memory database.

    Throws

    An error if the database could not be created

    Declaration

    Swift

    public init(label: String, qos: DispatchQoS = .default, target: DispatchQueue? = nil) throws

    Parameters

    label

    The label to attach to the queue

    qos

    The quality of service class for the work performed by the database queue

    target

    The target dispatch queue on which to execute blocks

  • Creates a database queue for serialized access to a database from a file.

    Throws

    An error if the database could not be opened

    Declaration

    Swift

    public init(url: URL, label: String, qos: DispatchQoS = .default, target: DispatchQueue? = nil) throws

    Parameters

    url

    The location of the SQLite database

    label

    The label to attach to the queue

    qos

    The quality of service class for the work performed by the database queue

    target

    The target dispatch queue on which to execute blocks

  • Creates a database queue for serialized access to an existing database.

    Attention

    The database queue takes ownership of database. The result of further use of database is undefined.

    Declaration

    Swift

    public init(database: Database, label: String, qos: DispatchQoS = .default, target: DispatchQueue? = nil)

    Parameters

    database

    The database to be serialized

    label

    The label to attach to the queue

    qos

    The quality of service class for the work performed by the database queue

    target

    The target dispatch queue on which to execute blocks

  • Performs a synchronous operation on the database.

    Throws

    Any error thrown in block

    Declaration

    Swift

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

    Parameters

    block

    A closure performing the database operation

    database

    A Database used for database access within block

    Return Value

    The value returned by block

  • Submits an asynchronous operation to the database queue.

    Declaration

    Swift

    public func async(group: DispatchGroup? = nil, qos: DispatchQoS = .default, block: @escaping (_ database: Database) -> (Void))

    Parameters

    group

    An optional DispatchGroup with which to associate block

    qos

    The quality of service for block

    block

    A closure performing the database operation

    database

    A Database used for database access within block

  • Performs a synchronous transaction on the database.

    Throws

    Any error thrown in block or an error if the transaction could not be started, rolled back, or committed

    Note

    If block throws an error the transaction will be rolled back and the error will be re-thrown

    Note

    If an error occurs committing the transaction a rollback will be attempted and the error will be re-thrown

    Declaration

    Swift

    public func transaction(type: Database.TransactionType = .deferred, _ block: Database.TransactionBlock) throws

    Parameters

    type

    The type of transaction to perform

    block

    A closure performing the database operation

  • Submits an asynchronous transaction to the database queue.

    Declaration

    Swift

    public func asyncTransaction(type: Database.TransactionType = .deferred, group: DispatchGroup? = nil, qos: DispatchQoS = .default, _ block: @escaping Database.TransactionBlock)

    Parameters

    type

    The type of transaction to perform

    group

    An optional DispatchGroup with which to associate block

    qos

    The quality of service for block

    block

    A closure performing the database operation

  • Performs a synchronous savepoint transaction on the database.

    Throws

    Any error thrown in block or an error if the savepoint could not be started, rolled back, or released

    Note

    If block throws an error the savepoint will be rolled back and the error will be re-thrown

    Note

    If an error occurs releasing the savepoint a rollback will be attempted and the error will be re-thrown

    Declaration

    Swift

    public func savepoint(block: Database.SavepointBlock) throws

    Parameters

    block

    A closure performing the database operation

  • Submits an asynchronous savepoint transaction to the database queue.

    Declaration

    Swift

    public func asyncSavepoint(group: DispatchGroup? = nil, qos: DispatchQoS = .default, block: @escaping Database.SavepointBlock)

    Parameters

    group

    An optional DispatchGroup with which to associate block

    qos

    The quality of service for block

    block

    A closure performing the database operation