DatabaseRegionObservation

public struct DatabaseRegionObservation

DatabaseRegionObservation tracks changes in the results of database requests, and notifies each database transaction whenever the database changes.

For example:

let observation = DatabaseRegionObservation(tracking: Player.all)
let observer = try observation.start(in: dbQueue) { db: Database in
    print("Players have changed.")
}
  • The extent of the database observation. The default is .observerLifetime: the observation lasts until the observer returned by the start(in:onChange:) method is deallocated.

    Declaration

    Swift

    public var extent: Database.TransactionObservationExtent
  • Creates a DatabaseRegionObservation which observes regions, and notifies whenever one of the observed regions is modified by a database transaction.

    For example, this sample code counts the number of a times the player table is modified:

    let observation = DatabaseRegionObservation(tracking: Player.all())
    
    var count = 0
    let observer = observation.start(in: dbQueue) { _ in
        count += 1
        print("Players have been modified \(count) times.")
    }
    

    The observation lasts until the observer returned by start is deallocated. See the extent property for more information.

    Declaration

    Swift

    public init(tracking regions: DatabaseRegionConvertible...)

    Parameters

    regions

    A list of observed regions.

  • Creates a DatabaseRegionObservation which observes regions, and notifies whenever one of the observed regions is modified by a database transaction.

    For example, this sample code counts the number of a times the player table is modified:

    let observation = DatabaseRegionObservation(tracking: [Player.all()])
    
    var count = 0
    let observer = observation.start(in: dbQueue) { _ in
        count += 1
        print("Players have been modified \(count) times.")
    }
    

    The observation lasts until the observer returned by start is deallocated. See the extent property for more information.

    Declaration

    Swift

    public init(tracking regions: [DatabaseRegionConvertible])

    Parameters

    regions

    A list of observed regions.

  • Starts the observation in the provided database writer (such as a database queue or database pool), and returns a transaction observer.

    Declaration

    Swift

    public func start(in dbWriter: DatabaseWriter, onChange: @escaping (Database) -> Void) throws -> TransactionObserver

    Parameters

    reader

    A DatabaseWriter.

    onChange

    A closure that is provided a database connection with write access each time the observed region has been modified.

    Return Value

    a TransactionObserver