0

What I'd like to do is access data at a few different URLs, combine this data, and then work with it in my app. I'm having no trouble getting all of the data I need, but what I can't figure out is how to combine the data into a single list and know when all of the API calls are done (all of the data is in the list).

Here's the loop where I make my API calls:

for(String diningCourt: diningCourts){
        menuApi.getMenu(diningCourt, date)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(responseData -> {

                     //I want to add responseData to a list with 
                     //the results from the rest of the calls 

                });
    }

What's the correct way to collect the data? I've tried using a static class variable to store the results but I only end up with data from one of the API calls. Any suggestions would be greatly appreciated.

1 Answer 1

1

We use "combineLatest" see http://reactivex.io/documentation/operators/combinelatest.html With this operator both calls are executed and you get a result, when both calls have finished.

Observable.combineLatest(observable1, observable2, (result1, result2) -> { //use both results })
Sign up to request clarification or add additional context in comments.

2 Comments

I'm having some trouble getting it to work. Is there something I need to do to the observables? Currently I have this and I can't get the print statement to show: Observable.combineLatest(observables.get(0), observables.get(1), (result1, result2) -> { System.out.println("HERE"); return null; }); (where observables is a list of Observable objects generated by the retrofit calls)
You have to subscribe to the observable. Otherwise the items are not pulled from the source.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.