Instance Methodgrdb 7.2.0GRDB

write(_:)

Executes database operations, and returns their result after they have finished executing.

DatabaseWriter.swift:416
func write<T>(_ updates: (Database) throws -> T) throws -> T

Parameters

updates

A closure which accesses the database.

Throws

The error thrown by updates, or any DatabaseError that would happen while establishing the database access or committing the transaction.

For example:

let newPlayerCount = try writer.write { db in
    try Player(name: "Arthur").insert(db)
    return try Player.fetchCount(db)
}

Database operations are wrapped in a transaction. If they throw an error, the transaction is rollbacked and the error is rethrown.

Concurrent database accesses can not see partial database updates (even when performed by other processes).

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.