Files
GRDB.swift/GRDB/ValueObservation/ValueObservation+Count.swift
T

71 lines
2.4 KiB
Swift

extension FetchRequest {
// MARK: - Observation
/// Creates a ValueObservation which observes *request*, and notifies its
/// count whenever it is modified by a database transaction.
///
/// For example:
///
/// let request = Player.all()
/// let observation = request.observationForCount()
///
/// let observer = try observation.start(
/// in: dbQueue,
/// onError: { error in ... },
/// onChange: { count: Int in
/// print("Number of players has changed")
/// })
///
/// The returned observation has the default configuration:
///
/// - When started with the `start(in:onError:onChange:)` method, a fresh
/// value is immediately notified on the main queue.
/// - Upon subsequent database changes, fresh values are notified on the
/// main queue.
/// - The observation lasts until the observer returned by
/// `start` is deallocated.
///
/// - returns: a ValueObservation.
public func observationForCount() ->
ValueObservation<ValueReducers.RemoveDuplicates<ValueReducers.Fetch<Int>>>
{
ValueObservation.tracking(fetchCount).removeDuplicates()
}
}
extension TableRecord {
// MARK: - Observation
/// Creates a ValueObservation which observes the record table, and notifies
/// its count whenever it is modified by a database transaction.
///
/// For example:
///
/// let observation = Player.observationForCount()
///
/// let observer = try observation.start(
/// in: dbQueue,
/// onError: { error in ... },
/// onChange: { count: Int in
/// print("Number of players has changed")
/// })
///
/// The returned observation has the default configuration:
///
/// - When started with the `start(in:onError:onChange:)` method, a fresh
/// value is immediately notified on the main queue.
/// - Upon subsequent database changes, fresh values are notified on the
/// main queue.
/// - The observation lasts until the observer returned by
/// `start` is deallocated.
///
/// - returns: a ValueObservation.
public static func observationForCount() ->
ValueObservation<ValueReducers.RemoveDuplicates<ValueReducers.Fetch<Int>>>
{
return all().observationForCount()
}
}