Sree
Sree

Reputation: 2787

If I am chaining multiple operators 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 for different Observables?

Upvotes: 5

Views: 769

Answers (1)

akarnokd
akarnokd

Reputation: 70007

No need for multiple subscribeOn calls; in this case, the second call is functionally a no-op but still holds onto some resources for the duration of the sequence. For example:

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

Will print something like ... RxCachedThreadScheduler-2

You probably need observeOn(Schedulers.computation()) that moves the observation of each value (a List object in this case) to another thread.

Upvotes: 5

Related Questions