RxJava 链中的多个 subscribeOn()
RxJava multiple subscribeOn() in chain
我想知道当我们在 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
我想知道当我们在 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