Home rxswift publishreplay
Post
Cancel

rxswift publishreplay

PublishRelay

PublishRelay is a wrapper for PublishSubject.

Unlike PublishSubject it can’t terminate with error or completed.

  • 作为observable, 可以订阅给subject
  • 可以通过subject发送next给observers
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public final class PublishRelay<Element>: ObservableType {
    private let _subject: PublishSubject<Element>
    
    // Accepts `event` and emits it to subscribers
    public func accept(_ event: Element) {
        self._subject.onNext(event)
    }
    
    /// Initializes with internal empty subject.
    public init() {
        self._subject = PublishSubject()
    }

    /// Subscribes observer
    public func subscribe<Observer: ObserverType>(_ observer: Observer) -> Disposable where Observer.Element == Element {
        return self._subject.subscribe(observer)
    }
    
    /// - returns: Canonical interface for push style sequence
    public func asObservable() -> Observable<Element> {
        return self._subject.asObservable()
    }
}

References

Subject

:文中源码来自RxSwift

This post is licensed under CC BY 4.0 by the author.