RxSwift學習筆記之Subject

本文爲原創文章,轉載請標明出處html

AsyncSubject

一個AsyncSubject只在原始Observable完成後,發射來自原始Observable的最後一個值。它會把這最後一個值發射給任何後續的觀察者。swift

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

當觀察者訂閱BehaviorSubject時,它開始發射原始Observable最近發射的數據,而後繼續發射其它任何來自原始Observable的數據。async

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)

輸出:code

observerA: 1
observerA: 2
observerB: 2
observerA: 3
observerB: 3

PublishSubject

PublishSubject只會把在訂閱發生的時間點以後來自原始Observable的數據發射給觀察者。server

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)

輸出:htm

observerA: 1
observerA: 2
observerB: 2

ReplaySubject

ReplaySubject在緩存增加到必定大小後會丟棄舊的數據,否則會發射全部來自原始Observable的數據給觀察者,不管它們是什麼時候訂閱的。get

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)

輸出:io

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
相關文章
相關標籤/搜索