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) throwsParameters
labelThe label to attach to the queue
qosThe quality of service class for the work performed by the database queue
targetThe 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) throwsParameters
urlThe location of the SQLite database
labelThe label to attach to the queue
qosThe quality of service class for the work performed by the database queue
targetThe 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 ofdatabaseis undefined.Declaration
Swift
public init(database: Database, label: String, qos: DispatchQoS = .default, target: DispatchQueue? = nil)Parameters
databaseThe database to be serialized
labelThe label to attach to the queue
qosThe quality of service class for the work performed by the database queue
targetThe target dispatch queue on which to execute blocks
-
Performs a synchronous operation on the database.
Throws
Any error thrown in
blockDeclaration
Swift
public func sync<T>(block: (_ database: Database) throws -> (T)) rethrows -> TParameters
blockA closure performing the database operation
databaseA
Databaseused for database access withinblockReturn 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
groupAn optional
DispatchGroupwith which to associateblockqosThe quality of service for
blockblockA closure performing the database operation
databaseA
Databaseused for database access withinblock -
Performs a synchronous transaction on the database.
Throws
Any error thrown in
blockor an error if the transaction could not be started, rolled back, or committedNote
If
blockthrows an error the transaction will be rolled back and the error will be re-thrownNote
If an error occurs committing the transaction a rollback will be attempted and the error will be re-thrown
Declaration
Parameters
typeThe type of transaction to perform
blockA closure performing the database operation
-
Submits an asynchronous transaction to the database queue.
Declaration
Parameters
typeThe type of transaction to perform
groupAn optional
DispatchGroupwith which to associateblockqosThe quality of service for
blockblockA closure performing the database operation
-
Performs a synchronous savepoint transaction on the database.
Throws
Any error thrown in
blockor an error if the savepoint could not be started, rolled back, or releasedNote
If
blockthrows an error the savepoint will be rolled back and the error will be re-thrownNote
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) throwsParameters
blockA 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
groupAn optional
DispatchGroupwith which to associateblockqosThe quality of service for
blockblockA closure performing the database operation
View on GitHub
DatabaseQueue Class Reference