WeakObserverBag

public actor WeakObserverBag

A bag which holds a list of weakly referenced observers.

  • Undocumented

    Declaration

    Swift

    public init()
  • Adds an observer to the bag.

    Declaration

    Swift

    public func add(observer: AnyObject)
  • Removes an observer from the bag.

    Declaration

    Swift

    public func remove(observer: AnyObject)
  • Notifies all observers by calling action on each observer.

    To ensure an observer only gets called on the main thread mark its delegate method with @MainActor:

    public protocol DelegateType: AnyObject, Sendable {
      @MainActor
      func notifyMethod()
    }
    

    Example how to use this method:

    await observerBag.notifyObservers { (delegate: DelegateType) in
      await delegate.notifyMethod()
    }
    

    Declaration

    Swift

    public func notifyObservers<T>(action: @escaping @Sendable (_ observer: T) async -> Void) async where T : Sendable

    Parameters

    action

    A closure which gets the casted observer passed. It’s the responsibility of the closure to call the corresponding delegate method.