Skip to content

Instantly share code, notes, and snippets.

@cavega
Created March 23, 2015 13:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cavega/7fea2c3160a4a6324794 to your computer and use it in GitHub Desktop.
Save cavega/7fea2c3160a4a6324794 to your computer and use it in GitHub Desktop.
Use RXJava for Merging Response of 2 API calls
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);
@erchenger
Copy link

Is the response from ApiProvider.requestSource1() and ApiProvider.requestSource2() the same or different?

@erchenger
Copy link

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.

@cavega
Copy link
Author

cavega commented Mar 23, 2015

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