combineLatest
ReactiveX:
when an item is emitted by either of two Observables, combine the latest item emitted by each Observable via a specified function and emit items based on the results of this function.
combineLatest
由多个可观测的值组成,并且当其中一个可观测的值发出数据时,combineLatest
将其组合起来,并订阅到结果。
let pb1 = PublishSubject<Int>()
let pb2 = PublishSubject<Int>()
Observable<String>.combineLatest(pb1, pb2) { (o1, o2) -> String in
return "First: " + "\(o1)" + ", Second:" + "\(o2)"
}.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)
pb2.onNext(2)
pb1.onNext(3)
输出:
element: First: 1, Second:2
element: First: 3, Second:2
第一次pb1
发出1
, pb2
发出2
,结果element: First: 1, Second:2
第二次pb1
发出3
, pb2
不发出, 结果 element: First: 3, Second:2
如果有一个源没有发出,就会继承上次的值。