unsafeRead(_:)
Executes database operations, and returns their result after they have finished executing.
func unsafeRead<T>(_ value: @escaping (Database) throws -> T) async throws -> T where T : Sendable
Parameters
- value
A closure which accesses the database.
Throws
Any DatabaseError
that happens while establishing the database access, or the error thrown by value
, or CancellationError
if the task is cancelled.
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 await 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.