Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RxSwift学习——(Zero) #99

Open
soapgu opened this issue Jan 14, 2022 · 0 comments
Open

RxSwift学习——(Zero) #99

soapgu opened this issue Jan 14, 2022 · 0 comments
Labels

Comments

@soapgu
Copy link
Owner

soapgu commented Jan 14, 2022

  • 前言

RxSwift是大名鼎鼎的ReactiveX异步框架的Swift版本。
目前我们已经基本上掌握了RxNet和RxJava两大语言的实现方式的用法,无论从框架的理解和应用上都有一定基础的。再说最近对Swift语言也有一定学习基础,所以可以适时推进下ReactiveX在Swift下的实现为以后项目打下基础
图片

首先从Github官网入口,
先简单过一下首页的README和GettingStarted

基本原理上不用再看了,可以直接以代码作为切入点最合适

最简单的就属于playground了,playground目前我还不是特别理解,是一个非常方便练手的“小品级”练手程序

怎么运行官方的playground

  1. git clone https://github.com/ReactiveX/RxSwift.git
  2. 打开Rx.xcworkspace
  3. 编译 build RxSwift-macOS 这个scheme
  4. 打开 RxExample -> Rx.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

参考

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,只能靠代码例子来形象化理解了

let disposeBag = DisposeBag()
    
    struct Player {
        init(score: Int) {
            self.score = BehaviorSubject(value: score)
        }

        let score: BehaviorSubject<Int>
    }
    
    let 👦🏻 = Player(score: 80)
    let 👧🏼 = Player(score: 90)
    
    let player = 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

会发现95被吃掉了,这就是区别

  • 为什么会被吃掉?

因为当前已经是👧🏼
二95是👦🏻.score
所以被忽略了

从数学角度来理解,就是平铺到“正投影”
再从效率角度来理解,flatMapLatest更有效率

我们假设投影每次都会产生一个无线循环到序列,flatMap会产生越来越多的无限循环序列(每第一层发射一次就多一条)
而flatMapLatest永远只有一个投影序列(每第一层发射一次,前一个序列就会被终止)
太形象了,这举栗子的人💯

参考资料
Transforming_Operators

  • 补充解释

再review一次Java的Rx的学习,看到了操作符 RxJAVA 的SwitchMap应该和RxSwift的flatMapLastest是等价的。

图片

上图。还是那句话:只可意会不可言传

参考资料

写得非常好,建议“翻墙”通读下加深理解

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant