SecureDatabaseQueue

public class SecureDatabaseQueue

Secure Database Queue Store

If you want to perform multiple database operations concurrently, you can create an instance of the SecureDatabaseQueue as follows.

var secureDatabaseQueue = SecureDatabaseQueue(databaseStore: secureDatabaseStore)

try secureDatabaseStore.open(with: "your_encryption_key")
  • Synchronously perform database operations on the queue.

    Note

    This method rethrows any errors that are not handled within block.

    Throws

    Any errors that are not handled within block.

    Sample

    do {
       try queue.inDatabase { db in
           let rs = try db.executeQuery("SELECT * from MyTable")
           defer {
               rs.close()
           }
           while try rs.next() {
               //...
           }
       }
    } catch let error {
       logger.error("An error occurred while executing updates.", error: error)
    }
    

    IMPORTANT: don’t call recursively. Don’t pass a closure which contains a call to the queue because it will end up in an critical failure.

    Declaration

    Swift

    public func inDatabase(_ block: @escaping (_ db: SecureDatabaseStore) throws -> Void) throws

    Parameters

    block

    The code to be run on the queue of SecureDatabaseQueue

  • Synchronously perform database operations on the queue, using exclusive transactions. Therefore, exclusive locks are acquired on the database and no other connection will be able to read or write to the database until the transaction is complete.

    Note

    This method rethrows any errors that are not handled within block.

    Throws

    Any errors that are not handled within block.

    Sample

    do {
       try queue.inExclusiveTransaction { db in
           do {
               try db.executeUpdate("INSERT INTO myTable VALUES (?)", values: [1])
               try db.executeUpdate("INSERT INTO myTable VALUES (?)", values: [2])
           } catch {
               return true // perform a rollback
           }
       }
    } catch let error {
       logger.error("An error occurred while executing updates.", error: error)
    }
    

    IMPORTANT: don’t call recursively. Don’t pass a closure which contains a call to the queue because it will end up in an critical failure.

    Declaration

    Swift

    public func inExclusiveTransaction(_ block: (_ db: SecureDatabaseStore) throws -> Bool) throws

    Parameters

    block

    The code to be run on the queue of SecureDatabaseQueue. It must return true if a rollback should be performed, false otherwise. A rollback reverts any database operation that have been performed in this block.

  • Synchronously perform database operations on the queue, using deferred transactions. Therefore, no locks are acquired on the database until the database is first accessed.

    Note

    This method rethrows any errors that are not handled within block.

    Throws

    Any errors that are not handled within block.

    Sample

    do {
       try queue.inDeferredTransaction { db in
           do {
               try db.executeUpdate("INSERT INTO myTable VALUES (?)", values: [1])
               try db.executeUpdate("INSERT INTO myTable VALUES (?)", values: [2])
           } catch {
               return true // perform a rollback
           }
           return false
       }
    } catch let error {
       logger.error("An error occurred while executing updates.", error: error)
    }
    

    IMPORTANT: don’t call recursively. Don’t pass a closure which contains a call to the queue because it will end up in a critical failure.

    Declaration

    Swift

    public func inDeferredTransaction(_ block: (_ db: SecureDatabaseStore) throws -> Bool) throws

    Parameters

    block

    The code to be run on the queue of SecureDatabaseQueue. It must return true if a rollback should be performed, false otherwise. A rollback reverts any database operation that have been performed in this block.