FlatMap家族
2017 - 07 - 19
代码
struct Player {
var score: BehaviorSubject<CustomStringConvertible>
}
let 👦🏻 = Player(score: BehaviorSubject(value: 0))
let 👧🏼 = Player(score: BehaviorSubject(value: "A"))
let player = Variable(👦🏻)
player.asObservable()
.flatMap { $0.score.asObservable() }
.subscribe(onNext: { print($0, terminator: "\t") })
.disposed(by: bag)
👦🏻.score.onNext(1)
player.value = 👧🏼
👧🏼.score.onNext("B")
👦🏻.score.onNext(2)
//flatMapFirst 时打开
//print("\nCompleted")
//👦🏻.score.onCompleted()
//player.value = 👧🏼
//👧🏼.score.onNext("C")
下面各方法替换后的输出
flatMap
0 1 A B 2
改变Value即增加一个流, 所有信号均收
flatMapLatest
0 1 A B
改变Value即切换流, 接收新流
注: 与switchLatest区别
.flatMapLatest(func) == .map(func).switchLatest()
flatMapFirst
0 1 2
Completed
B C
改变Value, 去检查之前流有无结束, 结束即开始新流, 未结束, 继续之前的流