I want to make 2 network calls asynchronously - I use Retrofit + RxJava to accomplish this. This logic consists of a simple Runner class to test the solution. NOTE. This applies mainly to server-side RxJava.
My code is as follows:
public static void main(String[] args) throws Exception { Api api = ...; Observable.combineLatest( api.getStates(), api.getCmsContent(), new Func2<List<States>, CmsContent, String>() { @Override public String call(List<State> states, CmsContent content) { ... return "PLACEHOLDER"; } }) .observeOn(Schedulers.immediate()) .subscribeOn(Schedulers.immediate()) .subscribe(new Observer<String>() { @Override public void onCompleted() { System.out.println("COMPLETED"); } @Override public void onError(Throwable e) { System.out.println("ERROR: " + e.getMessage()); } @Override public void onNext(String s) {
Three questions:
- Is
Observable.combineLatest best operator to use when you want to make multiple REST calls asynchronously and continue when all calls are completed? - The implementation of My
Func2 currently returns a String . After making two API calls, I will process the results in the Func2#call() method. I don’t care what comes back - there should be a better way to handle this, right, am I right? - API calls are correctly executed with the above code. But when the program starts, the
main method does not match the correct Process finished with exit code 0 . What can lead to code freezing?
UPDATE - 2015-05-14
Based on the recommendation, I changed the logic to the following:
public static void main(String[] args) throws Exception { Api api = ...; Observable.zip( api.getStates(), api.getCmsContent(), new Func2<List<States>, CmsContent, Boolean>() { @Override public Boolean call(List<State> states, CmsContent content) {
This seems like the solution I was looking for. I am going to use it for some time to see if I encountered any problems.
retrofit rx-java
Kasa
source share