DatabaseQueue

A DatabaseQueue serializes access to an SQLite database.

Configuration

  • The database configuration

  • The path to the database file; it is “:memory:” for in-memory databases.

Initializers

  • Opens the SQLite database at path path.

    let dbQueue = try DatabaseQueue(path: "/path/to/database.sqlite")
    

    Database connections get closed when the database queue gets deallocated.

    Throws

    A DatabaseError whenever an SQLite error occurs.
  • Opens an in-memory SQLite database.

    let dbQueue = try DatabaseQueue()
    

    Database memory is released when the database queue gets deallocated.

Memory management

  • Free as much memory as possible.

    This method blocks the current thread until all database accesses are completed.

Interrupting Database Operations

Writing in Database

  • Synchronously executes database updates in a protected dispatch queue, wrapped inside a transaction, and returns the result.

    If the updates throws an error, the transaction is rollbacked and the error is rethrown. If the updates return .rollback, the transaction is also rollbacked, but no error is thrown.

    Eventual concurrent database accesses are postponed until the transaction has completed.

    This method is not reentrant.

    try dbQueue.writeInTransaction { db in
        db.execute(...)
        return .commit
    }
    

    Throws

    The error thrown by the updates, or by the wrapping transaction.
  • Synchronously executes database updates in a protected dispatch queue, outside of any transaction, and returns the result.

    Eventual concurrent database updates are postponed until the updates are completed.

    Eventual concurrent reads may see partial updates unless you wrap them in a transaction.

    This method is not reentrant.

    Throws

    The error thrown by the updates.