unsafeRead(_:)
Executes database operations, and returns their result after they have finished executing.
func unsafeRead<T>(_ value: (Database) throws -> T) throws -> T
Parameters
- value
A closure which accesses the database.
Throws
The error thrown by value
, or any DatabaseError
that would happen while establishing the database access.
This method is “unsafe” because the database reader does nothing more than providing a database connection. When you use this method, you become responsible for the thread-safety of your application, and responsible for database accesses performed by other processes. See Safe and Unsafe Database Accesses for more information.
For example:
let count = try reader.unsafeRead { db in
try Player.fetchCount(db)
}
The Database
argument to value
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.