If I bind multiple statements using RxJava, do I need to call .subscribeOn () for each of them?

Here is an example:

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()) 

Do I need both .subscribeOn(Schedulers.computation()) and .subscribeOn(Schedulers.computation()) because they are designed for different observables?

+5
source share
1 answer

No need for multiple calls to subscribeOn ; in this case, the second call is functionally non-op, but still held on some resources for the duration of the sequence. For instance:

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

Print something like ... RxCachedThreadScheduler-2

You probably need to observeOn(Schedulers.computation()) , which moves the observation of each value (the List object in this case) to another stream.

+5
source

Source: https://habr.com/ru/post/1237872/


All Articles