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, callremoveObserver(:)
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 )
-
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
forObservable
object.Declaration
Swift
public func addObserver(_ observer: Observer)
Parameters
observer
Observer
to register. -
Unregister an
Observer
fromObservable
object.Declaration
Swift
public func removeObserver(_ observer: Observer)
Parameters
observer
Observer
to unregister.