You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
如果看不到运行结果请这样操作
To view the results of the examples in the playgrounds, please open the Assistant Editor. You can open Assistant Editor by clicking on View > Assistant Editor > Show Assistant Editor
接下来可以啃下flatMapLatest了
看概念
Transforms the elements emitted by an Observable sequence into Observable sequences, and merges the emissions from both Observable sequences into a single Observable sequence. This is also useful when, for example, when you have an Observable sequence that itself emits Observable sequences, and you want to be able to react to new emissions from either Observable sequence. The difference between flatMap and flatMapLatest is, flatMapLatest will only emit elements from the most recent inner Observable sequence
都是从一个Observable“平铺映射”到另外一个Observable
划重点:The difference between flatMap and flatMapLatest is, flatMapLatest will only emit elements from the most recent inner Observable sequence
还不太理解啥the most recent inner Observable sequence,只能靠代码例子来形象化理解了
letdisposeBag=DisposeBag()structPlayer{init(score:Int){self.score =BehaviorSubject(value: score)}letscore:BehaviorSubject<Int>}let👦🏻=Player(score:80)let👧🏼=Player(score:90)letplayer=BehaviorSubject(value: 👦🏻)
player.asObservable().flatMap{ $0.score.asObservable()} // Change flatMap to flatMapLatest and observe change in printed output
.subscribe(onNext:{print($0)}).disposed(by: disposeBag)
👦🏻.score.onNext(85)
player.onNext(👧🏼)
👦🏻.score.onNext(95) // Will be printed when using flatMap, but will not be printed when using flatMapLatest
👧🏼.score.onNext(100)}
代码很形象
player 是第一级的Observable,score 是第二级的Observable
player和score之间做了一次flatMap
看运行结果
--- flatMap and flatMapLatest example ---
80
85
90
95
100
换成flatMapLatest后
--- flatMap and flatMapLatest example ---
80
85
90
100
前言
RxSwift是大名鼎鼎的ReactiveX异步框架的Swift版本。
目前我们已经基本上掌握了RxNet和RxJava两大语言的实现方式的用法,无论从框架的理解和应用上都有一定基础的。再说最近对Swift语言也有一定学习基础,所以可以适时推进下ReactiveX在Swift下的实现为以后项目打下基础
RxNet
RxJava
入门
首先从Github官网入口,
先简单过一下首页的README和GettingStarted
README
GettingStarted
切入点
基本原理上不用再看了,可以直接以代码作为切入点最合适
最简单的就属于playground了,playground目前我还不是特别理解,是一个非常方便练手的“小品级”练手程序
怎么运行官方的playground
我们现在打开了Introduce这个playground
playground既包含了代码部分(有行号),也包含了文档部分
代码部分可以直接修改调试加断点,有点像学前端js代码类似
点击运行后可以直接看结果
如果看不到运行结果请这样操作
To view the results of the examples in the playgrounds, please open the Assistant Editor. You can open Assistant Editor by clicking on View > Assistant Editor > Show Assistant Editor
参考
playgrounds
flatMap and flatMapLatest
flatMapLatest是RxSwift的专有操作符(还有RxJS和RxPHP),有一点点有趣的区别
想要了解flatMapLatest需要先了解他的鼻祖,flatMap
flatMap就是做了一个Rx层次的SelectMany(类比Linq),做了一次事件的平铺展开,下面两个图有助理解
接下来可以啃下flatMapLatest了
看概念
Transforms the elements emitted by an Observable sequence into Observable sequences, and merges the emissions from both Observable sequences into a single Observable sequence. This is also useful when, for example, when you have an Observable sequence that itself emits Observable sequences, and you want to be able to react to new emissions from either Observable sequence. The difference between flatMap and flatMapLatest is, flatMapLatest will only emit elements from the most recent inner Observable sequence
都是从一个Observable“平铺映射”到另外一个Observable
划重点:The difference between flatMap and flatMapLatest is, flatMapLatest will only emit elements from the most recent inner Observable sequence
还不太理解啥the most recent inner Observable sequence,只能靠代码例子来形象化理解了
代码很形象
player 是第一级的Observable,score 是第二级的Observable
player和score之间做了一次flatMap
看运行结果
换成flatMapLatest后
会发现95被吃掉了,这就是区别
因为当前已经是👧🏼
二95是👦🏻.score
所以被忽略了
从数学角度来理解,就是平铺到“正投影”
再从效率角度来理解,flatMapLatest更有效率
我们假设投影每次都会产生一个无线循环到序列,flatMap会产生越来越多的无限循环序列(每第一层发射一次就多一条)
而flatMapLatest永远只有一个投影序列(每第一层发射一次,前一个序列就会被终止)
太形象了,这举栗子的人💯
参考资料
Transforming_Operators
补充解释
再review一次Java的Rx的学习,看到了操作符 RxJAVA 的SwitchMap应该和RxSwift的flatMapLastest是等价的。
上图。还是那句话:只可意会不可言传
参考资料
写得非常好,建议“翻墙”通读下加深理解
The text was updated successfully, but these errors were encountered: