本文為原創(chuàng)文章,轉(zhuǎn)載請(qǐng)標(biāo)明出處
AsyncSubject
一個(gè)AsyncSubject只在原始Observable完成后语婴,發(fā)射來自原始Observable的最后一個(gè)值。它會(huì)把這最后一個(gè)值發(fā)射給任何后續(xù)的觀察者。
let disposeBag = DisposeBag()
let subject = AsyncSubject<Int>()
subject.onNext(1)
subject.subscribe(onNext: { int in
print("observerA: \(int)")
}, onCompleted: {
print("observerA: onCompleted")
}).disposed(by: disposeBag)
subject.onNext(2)
subject.subscribe(onNext: { int in
print("observerB: \(int)")
}, onCompleted: {
print("observerB: onCompleted")
}).disposed(by: disposeBag)
subject.onNext(3)
subject.subscribe(onNext: { int in
print("observerC: \(int)")
}, onCompleted: {
print("observerC: onCompleted")
}).disposed(by: disposeBag)
subject.onCompleted()
subject.onNext(4)
subject.subscribe(onNext: { int in
print("observerD: \(int)")
}, onCompleted: {
print("observerD: onCompleted")
}).disposed(by: disposeBag)
輸出:
observerA: 3
observerB: 3
observerC: 3
observerA: onCompleted
observerB: onCompleted
observerC: onCompleted
observerD: 3
observerD: onCompleted
BehaviorSubject
當(dāng)觀察者訂閱BehaviorSubject時(shí)首有,它開始發(fā)射原始Observable最近發(fā)射的數(shù)據(jù),然后繼續(xù)發(fā)射其它任何來自原始Observable的數(shù)據(jù)枢劝。
let disposeBag = DisposeBag()
let subject = BehaviorSubject<Int>(value: 0)
subject.onNext(1)
subject.subscribe(onNext: { int in
print("observerA: \(int)")
}, onCompleted: {
print("observerA: onCompleted")
}).disposed(by: disposeBag)
subject.onNext(2)
subject.subscribe(onNext: { int in
print("observerB: \(int)")
}, onCompleted: {
print("observerB: onCompleted")
}).disposed(by: disposeBag)
subject.onNext(3)
輸出:
observerA: 1
observerA: 2
observerB: 2
observerA: 3
observerB: 3
PublishSubject
PublishSubject只會(huì)把在訂閱發(fā)生的時(shí)間點(diǎn)之后來自原始Observable的數(shù)據(jù)發(fā)射給觀察者井联。
let disposeBag = DisposeBag()
let subject = PublishSubject<Int>()
subject.onNext(0)
subject.subscribe(onNext: { int in
print("observerA: \(int)")
}, onCompleted: {
print("observerA: onCompleted")
}).disposed(by: disposeBag)
subject.onNext(1)
subject.subscribe(onNext: { int in
print("observerB: \(int)")
}, onCompleted: {
print("observerB: onCompleted")
}).disposed(by: disposeBag)
subject.onNext(2)
輸出:
observerA: 1
observerA: 2
observerB: 2
ReplaySubject
ReplaySubject在緩存增長到一定大小后會(huì)丟棄舊的數(shù)據(jù),不然會(huì)發(fā)射所有來自原始Observable的數(shù)據(jù)給觀察者您旁,無論它們是何時(shí)訂閱的烙常。
let disposeBag = DisposeBag()
let subject = ReplaySubject<Int>.create(bufferSize: 3)
subject.onNext(1)
subject.onNext(2)
subject.onNext(3)
subject.subscribe(onNext: { int in
print("observerA: \(int)")
}, onCompleted: {
print("observerA: onCompleted")
}).disposed(by: disposeBag)
subject.onNext(4)
subject.subscribe(onNext: { int in
print("observerB: \(int)")
}, onCompleted: {
print("observerB: onCompleted")
}).disposed(by: disposeBag)
subject.onCompleted()
subject.subscribe(onNext: { int in
print("observerC: \(int)")
}, onCompleted: {
print("observerC: onCompleted")
}).disposed(by: disposeBag)
輸出:
observerA: 1
observerA: 2
observerA: 3
observerA: 4
observerB: 2
observerB: 3
observerB: 4
observerA: onCompleted
observerB: onCompleted
observerC: 2
observerC: 3
observerC: 4
observerC: onCompleted