RxSwift 操作符 (concat)

concat

ReactiveX:
The Concat operator concatenates the output of multiple Observables so that they act like a single Observable, with all of the items emitted by the first Observable being emitted before any of the items emitted by the second Observable (and so forth, if there are more than two).

concat是按照顺序将超过两个源按顺序合并起来并发送出来。

let pb1 = PublishSubject<Int>()
let pb2 = PublishSubject<Int>()

pb1.concat(pb2)
    .subscribe { event in
        switch event {
        case .next(let element):
            print("element:", element)
        case .error(let error):
            print("error:", error)
        case .completed:
            print("completed")
        }}
    .disposed(by: bag)

pb1.onNext(1)
pb1.onNext(3)
pb2.onNext(2)
pb1.onCompleted()
pb2.onNext(4)

输出:
element: 1
element: 3
element: 4

pb1发出了1和3后,pb2发出了2,但是这个时候pb1调用了completed事件,所以pb2发出的2就会被忽略了,所以只有在pb1发出completed事件后,pb2的事件才不会被忽略。

猜你喜欢

转载自blog.csdn.net/weixin_38318852/article/details/80328405