inDatabase(_:)
Executes database operations, and returns their result after they have finished executing.
func inDatabase<T>(_ updates: (Database) throws -> T) rethrows -> T
Parameters
- updates
A closure which accesses the database.
Throws
The error thrown by updates
.
This method is identical to writeWithoutTransaction(_:)
For example:
let newPlayerCount = try dbQueue.inDatabase { db in
try Player(name: "Arthur").insert(db)
return try Player.fetchCount(db)
}
Database operations run in the writer dispatch queue, serialized with all database updates performed by this DatabaseWriter
.
The Database
argument to updates
is valid only during the execution of the closure. Do not store or return the database connection for later use.
It is a programmer error to call this method from another database access method. Doing so raises a “Database methods are not reentrant” fatal error at runtime.