gpt4 book ai didi

java - 如果我使用 RxJava 链接多个运算符,我需要为每个运算符调用 .subscribeOn() 吗?

转载 作者:搜寻专家 更新时间:2023-10-31 20:01:00 25 4
gpt4 key购买 nike

这是一个例子:

 return ApiClient.getPhotos()
.subscribeOn(Schedulers.io())
.map(new Func1<APIResponse<PhotosResponse>, List<Photo>>() {
@Override
public List<Photo> call(CruiselineAPIResponse<PhotosResponse> response) {
//convert the photo entities into Photo objects
List<ApiPhoto> photoEntities = response.getPhotos();
return Photo.getPhotosList(photoEntities);
}
})
.subscribeOn(Schedulers.computation())

我是否同时需要 .subscribeOn(Schedulers.computation()).subscribeOn(Schedulers.computation()) 因为它们用于不同的 Observables?

最佳答案

无需多次调用 subscribeOn;在这种情况下,第二个调用在功能上是空操作,但在序列持续期间仍会占用一些资源。例如:

Observable.just(1)
.map(v -> Thread.currentThread())
.subscribeOn(Schedulers.io())
.subscribeOn(Schedulers.computation())
.toBlocking()
.subscribe(System.out::println)

将打印类似... RxCachedThreadScheduler-2

您可能需要 observeOn(Schedulers.computation()) 将每个值(在本例中为 List 对象)的观察移动到另一个线程。

关于java - 如果我使用 RxJava 链接多个运算符,我需要为每个运算符调用 .subscribeOn() 吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34208947/

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