ReplaySubject

@propertyWrapper
public class ReplaySubject<Element> : Subject<Element>

A Subject that, in addition to normal emission and subscribe behavior, holds a cache of items and sends it, in order, to each new observer that is subscribed.

You can use it as a property wrapper to make the emitting private in the class where it’s contained, but still expose an Observable to the other classes.

  • Creates a replay subject with the specified cache size.

    Declaration

    Swift

    public init(cacheSize: Int?)

    Parameters

    cacheSize

    The maximum number of elements to cache. If nil is provided, there will be no maximum.

  • Creates a replay subject with a default cache size of 1.

    Declaration

    Swift

    convenience public override init()
  • Creates a replay subject with an initial value and cache size.

    Declaration

    Swift

    convenience public init(_ initialValue: Element, cacheSize: Int? = 1)

    Parameters

    initialValue

    The initial value to emit.

    cacheSize

    The maximum number of elements to cache. If nil is provided, there will be no maximum.

  • Returns an observable that replays cached elements to new subscribers.

    Declaration

    Swift

    public override func asObservable() -> Observable<Element>
  • Declaration

    Swift

    public override func subscribe<O>(_ observer: O) -> any Disposable where Element == O.Element, O : Observer
  • Emits an element to all subscribers and adds it to the cache.

    Declaration

    Swift

    public override func onNext(_ element: Element)
  • Removes all events from the cache.

    Declaration

    Swift

    public func clear()
  • Returns the last item that was emitted.

    Declaration

    Swift

    public func last() -> Element?
  • Changes the cache size removing oldest elements not fitting in.

    Declaration

    Swift

    public func resize(_ size: Int)
  • The wrapped observable value for property wrapper usage.

    Declaration

    Swift

    public override var wrappedValue: Observable<Element> { get }

Available where Element: Equatable

  • Emits the element only if it differs from the last cached value.

    Declaration

    Swift

    func onNextIfChanged(_ element: Element)