3

I use Retrofit 2 and I'm learning RxJava. I download a list of items from somewhere. I want to make an api call for each item and create a new list of extended items. Both calls return an Observable. It looks something like this:

getItemsService.getItems()
            .concatMap(Observable::from)
            .map(item -> new ExtendedItem(item, someOtherService.getDetails(item.somedetail)))
            .toList()
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(activity);

The details service:

public interface IGetDetailsService {
@GET(BuildConfig.GET_DETAILS_ENDPOINT)
Observable<Detail> getDetails(@Query("q") String someDetail);

}

The problem is that getDetails returns an Observable and I cannot use it to construct ExtendedItem. Changing the getDetails call to return an object does not work in the rxJava chain, I guess because it returns the result to the main thread. How do I get the object itself? I tried a lot of other things but nothing works so far.

1 Answer 1

2

You can use one of the variants of flatMap:

...
            .concatMap(Observable::from)
            .flatMap(item -> someOtherService.getDetails(item.somedetail), (item, detail) -> new ExtendedItem(item, detail))
            .toList()
...
Sign up to request clarification or add additional context in comments.

Comments

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.