mardi 5 juillet 2016

RxJava and Retrofit - Chain service response

New to RxJava. Trying to achieve something trivial but can't seem to wrap my head around the entire RxJava paradigm so far. All i want is to make two service calls one after the other and simply return a final custom POJO consisting of the response of the two calls. Point to be noted here is that the two calls are independent of each other. Only if the first calls comes back with a success, the second one should fire. The second one does not use any data from the first service call. What i have so far.

 RestWebClient.get().getFirstImage()
                .flatMap(firstImage -> RestWebClient.get().getSecondImage())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(firstImage -> mainFragmentPresenterInterface.showImages(firstImage));
  1. I am not sure how can i chain the response of two separate network calls and return them as a single POJO back to the presenter.

  2. Also how can we make sure that getSecondImage() is called only if getFirstImage() was a success?

Thanks.

Aucun commentaire:

Enregistrer un commentaire