DatabaseQueue
public final class DatabaseQueue : DatabaseWriter
A DatabaseQueue serializes access to an SQLite database.
-
The database configuration
Declaration
Swift
public var configuration: Configuration { get }
-
The path to the database file; it is “:memory:” for in-memory databases.
Declaration
Swift
public var path: String { get }
-
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.Declaration
Swift
public init(path: String, configuration: Configuration = Configuration()) throws
Parameters
path
The path to the database file.
configuration
A configuration.
-
Opens an in-memory SQLite database.
let dbQueue = DatabaseQueue()
Database memory is released when the database queue gets deallocated.
Declaration
Swift
public init(configuration: Configuration = Configuration())
Parameters
configuration
A configuration.
-
Declaration
Swift
public func close() throws
-
Free as much memory as possible.
This method blocks the current thread until all database accesses are completed.
Declaration
Swift
public func releaseMemory()
-
Declaration
Swift
public func interrupt()
-
Declaration
Swift
@_disfavoredOverload public func read<T>(_ value: (Database) throws -> T) throws -> T
-
Declaration
Swift
public func asyncRead(_ value: @escaping (Result<Database, Error>) -> Void)
-
Declaration
Swift
public func unsafeRead<T>(_ value: (Database) throws -> T) rethrows -> T
-
Declaration
Swift
public func asyncUnsafeRead(_ value: @escaping (Result<Database, Error>) -> Void)
-
Declaration
Swift
public func unsafeReentrantRead<T>(_ value: (Database) throws -> T) rethrows -> T
-
Declaration
Swift
public func concurrentRead<T>(_ value: @escaping (Database) throws -> T) -> DatabaseFuture<T>
-
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.Declaration
Parameters
kind
The transaction type (default nil). If nil, the transaction type is configuration.defaultTransactionKind, which itself defaults to .deferred. See https://www.sqlite.org/lang_transaction.html for more information.
updates
The updates to the database.
-
Declaration
Swift
@_disfavoredOverload public func writeWithoutTransaction<T>(_ updates: (Database) throws -> T) rethrows -> T
-
Declaration
Swift
@_disfavoredOverload public func barrierWriteWithoutTransaction<T>(_ updates: (Database) throws -> T) rethrows -> T
-
Declaration
Swift
public func asyncBarrierWriteWithoutTransaction(_ updates: @escaping (Database) -> Void)
-
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.Declaration
Swift
public func inDatabase<T>(_ updates: (Database) throws -> T) rethrows -> T
Parameters
updates
The updates to the database.
-
Declaration
Swift
public func unsafeReentrantWrite<T>(_ updates: (Database) throws -> T) rethrows -> T
-
Declaration
Swift
public func asyncWriteWithoutTransaction(_ updates: @escaping (Database) -> Void)