ObservableHandler

public final class ObservableHandler : Observable

A thread-safe implementation of the Observable container.

Any method can be called from any queue. The notification of observers, completions, and other closures will be executed on a dispatch queue provided in the init method.

Note

If you rely on the onLastObserverRemoved closure, call removeObserver(:) explicitly. If the observer is destroyed without an explicit call to removeObserver(:), the onLastObserverRemoved call will be delayed until the next notify event when we detect that the last observer is nil and remove it.
  • Create an ObservableHandler instance.

    Declaration

    Swift

    public init(
        dispatchQueue: DispatchQueue = .main,
        onFirstObserverAdded: (() -> ())? = nil,
        onLastObserverRemoved: (() -> ())? = nil
    )

    Parameters

    dispatchQueue

    DispatchQueue for events handling. DispatchQueue.main is the default value.

    onFirstObserverAdded

    Closure called when the first Observer is registered.

    onLastObserverRemoved

    Closure called when the last Observer is unregistered.

  • Notify observers about some specific event.

    Declaration

    Swift

    public func notifyObservers(notificationAction: @escaping (Observer) -> (), completion: (() -> ())?)

    Parameters

    notificationAction

    Use the closure to define what event every observer will receive. This method will apply the notificationAction closure to every registered observer.

    completion

    Called when the notificationAction closure is applied to all the registered observers.

  • Notify observers about some specific event.

    Declaration

    Swift

    public func notifyObservers(notificationAction: @escaping (Observer) -> ())

    Parameters

    notificationAction

    Use the closure to define what event every observer will receive. This method will apply the notificationAction closure to every registered observer.

  • Register an Observer for Observable object.

    Note

    Observer is kept with a weak pointer.

    Declaration

    Swift

    public func addObserver(_ observer: Observer)

    Parameters

    observer

    Observer to register.

  • Unregister an Observer from Observable object.

    Declaration

    Swift

    public func removeObserver(_ observer: Observer)

    Parameters

    observer

    Observer to unregister.