Swift学习

RXSwift - 合并 Observable (combine

2020-08-21  本文已影响0人  内蒙小猿

        let bag = DisposeBag ()
        
        let queueA = PublishSubject<String>()
        let queueB = PublishSubject<String>()
        
        let sequece = Observable.combineLatest(queueA, queueB)
        sequece.subscribe(onNext: { (str) in
            print(str)
        }, onError: nil, onCompleted: {
            print("onCompleted")
        }) {
            print("onDisposed")
        }.disposed(by: bag)

一、combineLast 把多个observables 中的 \color{#008080}{当前事件} 合成一个事件,最多接收7个参数,也可以合并事件类型不同的 Sub Observables。

        queueA.onNext("A1")
        queueB.onNext("B1")
        queueA.onNext("A2")
        queueB.onNext("B2")
    
        queueA.onCompleted()
        queueB.onCompleted()

1.1、执行过程

queueA
---------------A1-----------A2--------------------
queueB
---------------B1-----------B2--------------------
         
          combineLast(queueA , queueB)
         
sequeue
***********(A1,B1)*****(A2,B1)*****(A2,B2)*********

\color{#DEB887}{执行结果:("A1", "B1") -> ("A2", "B1") -> ("A2", "B2") -> onCompleted -> onDisposed}

1.2、note

二、zip: 只合并\color{#008080}{最新}事件,它的用法和combineLatest 几乎一样。

        
        queueA.onNext("A1")
        queueB.onNext("B1")
        queueA.onNext("A2")
        queueB.onNext("B2")
    
        queueA.onCompleted()
        queueB.onCompleted()

2.1 执行过程

queueA
---------------A1-----------A2--------------------
queueB
---------------B1-----------B2--------------------
         
          zip(queueA , queueB)
         
sequeue
***********(A1,B1)*****(A2,B2)********************

\color{#DEB887}{执行结果:("A1", "B1") -> ("A2", "B2") -> onCompleted -> onDisposed}

2.2 note

上一篇下一篇

猜你喜欢

热点阅读