Instance Methodgrdb 7.1.0GRDB

asyncConcurrentRead(_:)

Performs an asynchronous read access.

DatabasePool.swift:562
func asyncConcurrentRead(_ value: @escaping (Result<Database, Error>) -> Void)

Parameters

value

A function that accesses the database.

This method must be called from the writer dispatch queue, outside of any transaction. You’ll get a fatal error otherwise.

The value function is guaranteed to see the database in the last committed state at the moment this method is called. Eventual concurrent database updates are not visible from the function.

This method returns as soon as the isolation guarantee described above has been established.

In the example below, the number of players is fetched concurrently with the player insertion. Yet it is guaranteed to be zero:

try writer.asyncWriteWithoutTransaction { db in
    // Delete all players
    try Player.deleteAll()

    // Count players concurrently
    writer.asyncConcurrentRead { dbResult in
        do {
            let db = try dbResult.get()
            // Guaranteed to be zero
            let count = try Player.fetchCount(db)
        } catch {
            // Handle error
        }
    }

    // Insert a player
    try Player(...).insert(db)
}