AnyValueReducer

public struct AnyValueReducer<Fetched, Value> : ValueReducer

Experimental

A type-erased ValueReducer.

An AnyValueReducer forwards its operations to an underlying reducer, hiding its specifics.

  • Creates a reducer whose fetch(_:) and value(_:) methods wrap and forward operations the argument closures.

    For example, this reducer 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 observer = ValueObservation
        .tracking(Player.all(), reducer: reducer)
        .start(in: dbQueue) { count: Int in
            print("Players have been modified \(count) times.")
        }
    

    Declaration

    Swift

    public init(fetch: @escaping (Database) throws -> Fetched, value: @escaping (Fetched) -> Value?)
  • Creates a reducer that wraps and forwards operations to reducer.

    Declaration

    Swift

    public init<Base>(_ reducer: Base) where Fetched == Base.Fetched, Value == Base.Value, Base : ValueReducer