ReplaySubject

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

A Subject that, in addition to normal publish 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 publishing 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 publish.

    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>
  • Publishes an element and adds it to the cache.

    Declaration

    Swift

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

    Declaration

    Swift

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

    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

  • Publishes the new event only if the new one is different from the last one

    Declaration

    Swift

    func publishIfChanged(_ element: Element)