Observable

public class Observable<T>

The GGNObservable class can be used for simple reactive style programming. This class has a generic type constraint.

  • This typealias exists mainly for convenience, however it’s type is important. Closure is an optional function that takes an instance of the generic type T and returns Void.

    Returns

    Void

    Declaration

    Swift

    public typealias Closure = ((T) -> Void)

    Parameters

    T

    A function that takes a T

    Return Value

    Void

  • Initializes an instance of an Obersvable with a generic type T.

    • Example: let alertOutput = Observable<UIAlertController>()

    Declaration

    Swift

    public init()
  • Call this method on an instance of Observable to respond to events emitted from it. This method captures the passed in closure, stores it, and performs it when emit(event:) is called on the same instance of Observable. One Observable can have multiple observers.

    • Example: viewModel.alertOutput.onNext { [weak self] alert in self?.presentViewController(alert, animated: true, completion: nil) }

    Declaration

    Swift

    public func onNext(perform closure: Closure)

    Parameters

    closure

    The closure to perform on emit(event:).

  • Call this method on an instance of Observable to emit an instance of the generic type T. This method performs any and all closures that are captured by calls to onNext(perform:).

    • Example: alertOutput.emit(alert)

    Declaration

    Swift

    public func emit(event: T)

    Parameters

    event

    An instance of the generic type T.