Required Instance Methodgrdb 7.2.0GRDB

interrupt

Causes any pending database operation to abort and return at its earliest opportunity.

DatabaseReader.swift:155
func interrupt()

This method can be called from any thread.

A call to interrupt() that occurs when there are no running SQL statements is a no-op and has no effect on SQL statements that are started after interrupt() returns.

A database operation that is interrupted will throw a DatabaseError with code SQLITE_INTERRUPT. If the interrupted SQL operation is an INSERT, UPDATE, or DELETE that is inside an explicit transaction, then the entire transaction will be rolled back automatically. If the rolled back transaction was started by a transaction-wrapping method such as write(_:) or inTransaction(_:_:), then all database accesses will throw a DatabaseError with code SQLITE_ABORT until the wrapping method returns.

For example:

try dbQueue.write { db in
    // interrupted:
    try Player(...).insert(db)     // throws SQLITE_INTERRUPT
    // not executed:
    try Player(...).insert(db)
}                                  // throws SQLITE_INTERRUPT

try dbQueue.write { db in
    do {
        // interrupted:
        try Player(...).insert(db) // throws SQLITE_INTERRUPT
    } catch { }
    try Player(...).insert(db)     // throws SQLITE_ABORT
}                                  // throws SQLITE_ABORT

try dbQueue.write { db in
    do {
        // interrupted:
        try Player(...).insert(db) // throws SQLITE_INTERRUPT
    } catch { }
}                                  // throws SQLITE_ABORT

Beware: when an application opens a transaction without a transaction-wrapping method, no SQLITE_ABORT error warns of aborted transactions:

try dbQueue.inDatabase { db in // or dbPool.writeWithoutTransaction
    try db.beginTransaction()
    do {
        // interrupted:
        try Player(...).insert(db) // throws SQLITE_INTERRUPT
    } catch { }
    try Player(...).insert(db)     // success
    try db.commit()                // throws SQLITE_ERROR "cannot commit - no transaction is active"
}

Both SQLITE_ABORT and SQLITE_INTERRUPT errors can be checked with the isInterruptionError property.