注意:使用本例中的代码首先应该导入头文件,代码如下:
import RxSwift
Combination Operators
联合操作使多个源Observable
结合成一个Observable
。
startWith
在源Observable
序列开始发散元素之前发散指定元素的序列。了解更多
example("startWith") {
let disposeBag = DisposeBag()
Observable.of("🐶", "🐱", "🐭", "🐹")
.startWith("1")
.startWith("2")
.startWith("3", "A", "B")
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
Debug Area 输出:
--- startWith example ---
3
A
B
2
1
🐶
🐱
🐭
🐹
正如这个例子所示,
startWith
体现了后进先出的原则,那就是说,每一个连续的startWith
元素将会在优先的startWith
元素之前被考虑。
merge
合并多个源Observable
序列中的元素成为一个新的Observable
序列,并且将会发散每一个元素,相当于通过每个源Observable
序列发散的。�了解更多
example("merge") {
let disposeBag = DisposeBag()
let subject1 = PublishSubject<String>()
let subject2 = PublishSubject<String>()
Observable.of(subject1, subject2)
.merge()
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
subject1.onNext("A")
subject1.onNext("B")
subject2.onNext("①")
subject2.onNext("②")
subject1.onNext("🆎")
subject2.onNext("③")
}
Debug Area 输出:
--- merge example ---
A
B
①
②
🆎
③
zip
将多达8个源Observable
序列组合成一个新的Observable
序列,并且将会结合每个源Observable
序列中一致的索引位置的元素从新的Observable
序列中发散出去。了解更多
example("zip") {
let disposeBag = DisposeBag()
let stringSubject = PublishSubject<String>()
let intSubject = PublishSubject<Int>()
Observable.zip(stringSubject, intSubject) { stringElement, intElement in
"\(stringElement) \(intElement)"
}
.subscribeNext { print($0) }
.disposed(by: disposeBag)
stringSubject.onNext("A")
stringSubject.onNext("B")
intSubject.onNext(1)
intSubject.onNext(2)
stringSubject.onNext("🆎")
intSubject.onNext(3)
}
Debug Area 输出:
--- zip example ---
A ** 1**
�B ** 2**
🆎 ** 3**
combineLatest
将多达8个源Observable
组合成一个新的Observable
序列,并且一旦所有源序列发出至少一个�元素以及当任何源Observable
序列发出一个新的元素的时候,将开始发散组合的Observable
序列的最�新的元素。了解更多
example("combineLatest") {
let disposeBag = DisposeBag()
let stringSubject = PublishSubject<String>()
let intSubject = PublishSubject<Int>()
Observable.combineLatest(stringSubject, intSubject) { stringElement, intElement in
"\(stringElement) \(intElement)"
}
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
stringSubject.onNext("A")
stringSubject.onNext("B")
intSubject.onNext(1)
intSubject.onNext(2)
stringSubject.onNext("🆎")
}
Debug Area 输出:
--- combineLatest example ---
A ** 1**
�B ** 2**
A�B ** 2**
还有一个在Array
之上的combineLatest
扩展:
example("Array.combineLatest") {
let disposeBag = DisposeBag()
let stringObservable = Observable.just("💙")
let fruitObservable = Observable.from(["🍎", "🍐", "🍊"])
let animalObservable = Observable.of("🐶", "🐱", "🐭", "🐹")
[stringObservable, fruitObservable, animalObservable].combineLatest {
"\($0[0]) \($0[1]) \($0[2])"
}
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
}
Debug Area 输出:
--- Array.combineLatest example ---
💙** 🍎 🐶
💙 🍐 🐶
💙 🍐 🐱
💙 🍊 🐱
💙 🍊 🐭
💙 🍊 **🐹
Array
之上的combineLatest
扩展要求所有源Observable
序列的类型是相同的。
switchLatest
将一个Observable
序列发出的元素转换成多个Observable
序列,并且发出最近的Observable
序列内部的元素。了解更多
example("switchLatest") {
let disposeBag = DisposeBag()
let subject1 = BehaviorSubject(value: "⚽️")
let subject2 = BehaviorSubject(value: "🍎")
let variable = Variable(subject1)
variable.asObservable()
.switchLatest()
.subscribe(onNext: { print($0) })
.disposed(by: disposeBag)
subject1.onNext("🏈")
subject1.onNext("🏀")
variable.value = subject2
subject1.onNext("🏐")
subject2.onNext("🍐")
}
Debug Area 输出:
--- switchLatest example ---
⚽️
🏈
🏀
🍎
🍐
在这个例子中,在�把
variable.value
赋值给subject2
之后,在subject1
中添加🏐没有意义,因为只有最近的内在Observable
序列 (subject2
) 将会发出元素。