gpt4 book ai didi

android - RxJava 链中的多个 subscribeOn()

转载 作者:行者123 更新时间:2023-11-29 18:29:57 24 4
gpt4 key购买 nike

我想知道当我们在 RxJava 链中有多个 subscribeOn() 方法时会发生什么。例如,如果我有一个像这样的链,

    Single.fromCallable { repository.apiCall1() }
.subscribeOn(Schedulers.io())
.flatMap { result -> Single.fromCallable { repository.apiCall2() } }
.subscribeOn(Schedulers.io())
.map { // do something }
.observeOn(Schedulers.ui())

这是否意味着运行 API 调用的线程不同?删除上面这个链中的第一个 subscribeOn(Schedulers.io()) 调用会产生什么影响/效果?

感谢对此的任何见解

最佳答案

看起来不会有任何影响。将 subscribeOn 放在链中的什么位置都不会产生相同的效果。

根据文档,observeOn 具有不同的行为,因此您可以在链中的任何一点更改您观察结果的线程。

来自 Rx 文档:

the SubscribeOn operator designates which thread the Observable will begin operating on, no matter at what point in the chain of operators that operator is called. ObserveOn, on the other hand, affects the thread that the Observable will use below where that operator appears. For this reason, you may call ObserveOn multiple times at various points during the chain of Observable operators in order to change on which threads certain of those operators operate.

http://reactivex.io/documentation/operators/subscribeon.html

关于android - RxJava 链中的多个 subscribeOn(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56816393/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com