Created
March 23, 2015 13:50
-
-
Save cavega/7fea2c3160a4a6324794 to your computer and use it in GitHub Desktop.
Use RXJava for Merging Response of 2 API calls
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Observable.combineLatest( | |
ApiProvider.requestSource1(param1), | |
ApiProvider.requestSource2(param2), | |
this::combinedResponses) | |
.map(responseObject -> saveResponse(responseObject, context)) | |
.subscribeOn(Schedulers.io()) | |
.observeOn(AndroidSchedulers.mainThread()) | |
.subscribe(responseObject -> onResponseReceived(responseObject, listener), | |
null != errorCallback ? errorCallback : this::onFetchObjectFailed); |
Also this is where the observable that is omitted is a single source. For a list of data you would have to iterate over each source which is what I am trying to avoid. What I was looking at was having an iterable observable and merging the two data types for it.
The 2 responses are different. In my example, I'm sending the response "responseObject" from "source 1" into the map so that it can be used by the callback of "source 2" once the latter returns successfully. My use case was that I needed to merge the 2nd response with the results of the first 1 into a single object.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is the response from
ApiProvider.requestSource1()
andApiProvider.requestSource2()
the same or different?