mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-27 04:14:21 +00:00
ValueObservation: support region arrays (address #441)
This commit is contained in:
1 parent
d3fd838c6c
commit
b9ab74cb1d
1 file changed
+110
@@ -526,6 +526,48 @@ public struct ValueObservation<Reducer> {
|
||||
_ regions: DatabaseRegionConvertible...,
|
||||
reducer: Reducer)
|
||||
-> ValueObservation
|
||||
{
|
||||
return ValueObservation.tracking(regions, reducer: reducer)
|
||||
}
|
||||
|
||||
/// Returs a ValueObservation which observes *regions*, and notifies the
|
||||
/// values returned by the *reducer* whenever one of the observed
|
||||
/// regions is modified by a database transaction.
|
||||
///
|
||||
/// This method is the most fundamental way to create a ValueObservation.
|
||||
///
|
||||
/// For example, this observation counts the number of a times the player
|
||||
/// table is modified:
|
||||
///
|
||||
/// var count = 0
|
||||
/// let reducer = AnyValueReducer(
|
||||
/// fetch: { _ in },
|
||||
/// value: { _ -> Int? in
|
||||
/// count += 1
|
||||
/// return count
|
||||
/// })
|
||||
/// let observation = ValueObservation.tracking([Player.all()], reducer: reducer)
|
||||
/// let observer = observation.start(in: dbQueue) { count: Int in
|
||||
/// print("Players have been modified \(count) times.")
|
||||
/// }
|
||||
///
|
||||
/// 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.
|
||||
///
|
||||
/// - parameter regions: A list of observed regions.
|
||||
/// - parameter reducer: A reducer that turns database changes in the
|
||||
/// modified regions into fresh values. Currently only reducers that adopt
|
||||
/// the ValueReducer protocol are supported.
|
||||
public static func tracking(
|
||||
_ regions: [DatabaseRegionConvertible],
|
||||
reducer: Reducer)
|
||||
-> ValueObservation
|
||||
{
|
||||
return ValueObservation(tracking: union(regions), reducer: reducer)
|
||||
}
|
||||
@@ -573,6 +615,39 @@ extension ValueObservation where Reducer == Void {
|
||||
_ regions: DatabaseRegionConvertible...,
|
||||
fetch: @escaping (Database) throws -> Value)
|
||||
-> ValueObservation<ValueReducers.Raw<Value>>
|
||||
{
|
||||
return ValueObservation.tracking(regions, fetch: fetch)
|
||||
}
|
||||
|
||||
/// Creates a ValueObservation which observes *regions*, and notifies the
|
||||
/// values returned by the *fetch* closure whenever one of the observed
|
||||
/// regions is modified by a database transaction.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// let observation = ValueObservation.tracking(
|
||||
/// [Player.all()],
|
||||
/// fetch: { db in return try Player.fetchAll(db) })
|
||||
///
|
||||
/// let observer = try observation.start(in: dbQueue) { player: [Player] in
|
||||
/// print("Players have 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.
|
||||
///
|
||||
/// - parameter regions: A list of observed regions.
|
||||
/// - parameter fetch: A closure that fetches a value.
|
||||
public static func tracking<Value>(
|
||||
_ regions: [DatabaseRegionConvertible],
|
||||
fetch: @escaping (Database) throws -> Value)
|
||||
-> ValueObservation<ValueReducers.Raw<Value>>
|
||||
{
|
||||
return ValueObservation<ValueReducers.Raw<Value>>(
|
||||
tracking: union(regions),
|
||||
@@ -610,6 +685,41 @@ extension ValueObservation where Reducer == Void {
|
||||
fetchDistinct fetch: @escaping (Database) throws -> Value)
|
||||
-> ValueObservation<ValueReducers.Distinct<Value>>
|
||||
where Value: Equatable
|
||||
{
|
||||
return ValueObservation.tracking(regions, fetchDistinct: fetch)
|
||||
}
|
||||
|
||||
/// Creates a ValueObservation which observes *regions*, and notifies the
|
||||
/// values returned by the *fetch* closure whenever one of the observed
|
||||
/// regions is modified by a database transaction. Consecutive equal values
|
||||
/// are filtered out.
|
||||
///
|
||||
/// For example:
|
||||
///
|
||||
/// let observation = ValueObservation.tracking(
|
||||
/// [Player.all()],
|
||||
/// fetchDistinct: { db in return try Player.fetchAll(db) })
|
||||
///
|
||||
/// let observer = try observation.start(in: dbQueue) { player: [Player] in
|
||||
/// print("Players have 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.
|
||||
///
|
||||
/// - parameter regions: A list of observed regions.
|
||||
/// - parameter fetch: A closure that fetches a value.
|
||||
public static func tracking<Value>(
|
||||
_ regions: [DatabaseRegionConvertible],
|
||||
fetchDistinct fetch: @escaping (Database) throws -> Value)
|
||||
-> ValueObservation<ValueReducers.Distinct<Value>>
|
||||
where Value: Equatable
|
||||
{
|
||||
return ValueObservation<ValueReducers.Distinct<Value>>(
|
||||
tracking: union(regions),
|
||||
|
||||
Reference in new issue
Block a user