Observable

public protocol Observable : AnyObject

Protocol that allows an object to be observed by other objects.

When conforming to this protocol, make sure to invoke the notifyObservers method once the conforming object has been updated.

  • Declaration

    Swift

    associatedtype Observable
  • The list of identifiers linked to objects watching this observable

    Declaration

    Swift

    var onUpdate: ([UUID : (Observable) -> Void]) { get set }
  • Allows an object T to observe changes made to an object that conforms to the protocol

    Default Implementation

    Declaration

    Swift

    func addObserver<T: AnyObject>(_ observer: T,
                                   eagerObserver: Observable?,
                                   onRemove: ((UUID) -> Void)?,
                                   _ closure: @escaping (T?, UUID, Observable) -> Void) -> ObservationToken

    Parameters

    observer

    AnyObject that wants to observe an object that conforms to the protocol

    eagerObserver

    Observable type. When passed, the observer will get update on subscription

    closure

    A closure that passes through the object under observation, and the object observing it.

    Return Value

    A token that can be used to cancel the observation

  • notifyObservers(_:) Default implementation

    This should be called once the observable has changed/updated

    Default Implementation

    Declaration

    Swift

    func notifyObservers(_ observable: Observable)

    Parameters

    observable

    the observable object self.