DatabaseQueue
public final class DatabaseQueueA DatabaseQueue serializes access to an SQLite database.
- 
                  
                  The database configuration DeclarationSwift public var configuration: Configuration
- 
                  
                  The path to the database file; it is :memory: for in-memory databases.DeclarationSwift public var path: String
- 
                  
                  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.DeclarationSwift public init(path: String, configuration: Configuration = Configuration()) throwsParameterspathThe path to the database file. configurationA configuration. 
- 
                  
                  Opens an in-memory SQLite database. let dbQueue = DatabaseQueue()Database memory is released when the database queue gets deallocated. DeclarationSwift public init(configuration: Configuration = Configuration())ParametersconfigurationA configuration. 
- 
                  
                  Synchronously executes a block in a protected dispatch queue, and returns its result. let players = try dbQueue.inDatabase { db in try Player.fetchAll(...) }This method is not reentrant. Throws The error thrown by the block.DeclarationSwift public func inDatabase<T>(_ block: (Database) throws -> T) rethrows -> TParametersblockA block that accesses the database. 
- 
                  
                  Synchronously executes a block in a protected dispatch queue, wrapped inside a transaction. If the block throws an error, the transaction is rollbacked and the error is rethrown. If the block returns .rollback, the transaction is also rollbacked, but no error is thrown. try dbQueue.inTransaction { db in db.execute(...) return .commit }This method is not reentrant. Throws The error thrown by the block.DeclarationParameterskindThe transaction type (default nil). If nil, the transaction type is configuration.defaultTransactionKind, which itself defaults to .immediate. See https://www.sqlite.org/lang_transaction.html for more information. blockA block that executes SQL statements and return either .commit or .rollback. 
- 
                  
                  Free as much memory as possible. This method blocks the current thread until all database accesses are completed. See also setupMemoryManagement(application:) DeclarationSwift public func releaseMemory()
- 
                  
                  Listens to UIApplicationDidEnterBackgroundNotification and UIApplicationDidReceiveMemoryWarningNotification in order to release as much memory as possible. - param application: The UIApplication that will start a background task to let the database queue release its memory when the application enters background.
 DeclarationSwift public func setupMemoryManagement(in application: UIApplication)
- 
                  
                  Synchronously executes a read-only block in a protected dispatch queue, and returns its result. let players = try dbQueue.read { db in try Player.fetchAll(...) }This method is not reentrant. Starting iOS 8.2, OSX 10.10, and with custom SQLite builds and SQLCipher, attempts to write in the database throw a DatabaseError whose resultCode is SQLITE_READONLY.Throws The error thrown by the block.DeclarationSwift public func read<T>(_ block: (Database) throws -> T) rethrows -> TParametersblockA block that accesses the database. 
- 
                  
                  Alias for inDatabase. SeeDatabaseReader.unsafeRead.DeclarationSwift public func unsafeRead<T>(_ block: (Database) throws -> T) rethrows -> T
- 
                  
                  Synchronously executes a block in a protected dispatch queue, and returns its result. try dbQueue.unsafeReentrantRead { db in try db.execute(...) }This method is reentrant. It should be avoided because it fosters dangerous concurrency practices. DeclarationSwift public func unsafeReentrantRead<T>(_ block: (Database) throws -> T) throws -> T
- 
                  
                  Add or redefine an SQL function. let fn = DatabaseFunction("succ", argumentCount: 1) { dbValues in guard let int = Int.fromDatabaseValue(dbValues[0]) else { return nil } return int + 1 } dbQueue.add(function: fn) try dbQueue.inDatabase { db in try Int.fetchOne(db, "SELECT succ(1)") // 2 }DeclarationSwift public func add(function: DatabaseFunction)
- 
                  
                  Remove an SQL function. DeclarationSwift public func remove(function: DatabaseFunction)
- 
                  
                  Add or redefine a collation. let collation = DatabaseCollation("localized_standard") { (string1, string2) in return (string1 as NSString).localizedStandardCompare(string2) } dbQueue.add(collation: collation) try dbQueue.inDatabase { db in try db.execute("CREATE TABLE files (name TEXT COLLATE LOCALIZED_STANDARD") }DeclarationSwift public func add(collation: DatabaseCollation)
- 
                  
                  Remove a collation. DeclarationSwift public func remove(collation: DatabaseCollation)
- 
                  
                  Alias for inDatabase. SeeDatabaseWriter.write.DeclarationSwift public func write<T>(_ block: (Database) throws -> T) rethrows -> T
- 
                  
                  Synchronously executes block. Starting iOS 8.2, OSX 10.10, and with custom SQLite builds and SQLCipher, attempts to write in the database throw a DatabaseError whose resultCode is SQLITE_READONLY.This method must be called from the protected database dispatch queue, outside of a transaction. You’ll get a fatal error otherwise. See DatabaseWriter.readFromCurrentState.DeclarationSwift public func readFromCurrentState(_ block: @escaping (Database) -> Void)
- 
                  
                  Synchronously executes a block in a protected dispatch queue, and returns its result. try dbQueue.unsafeReentrantWrite { db in try db.execute(...) }This method is reentrant. It should be avoided because it fosters dangerous concurrency practices. DeclarationSwift public func unsafeReentrantWrite<T>(_ block: (Database) throws -> T) rethrows -> T
- 
                  
                  Listens to UIApplicationDidEnterBackgroundNotification and UIApplicationDidReceiveMemoryWarningNotification in order to release as much memory as possible. - param application: The UIApplication that will start a background task to let the database queue release its memory when the application enters background.
 
 View on GitHub
View on GitHub Install in Dash
Install in Dash DatabaseQueue Class Reference
        DatabaseQueue Class Reference