I.S
I.S

Reputation: 2053

How can I combine two object in one of the object in RxJava?

I have Data and DataDetail classes, each of them contains data that neighter of them don’t contain. I have List class which I fetch from backend, having Data which has id field I can request DataDetail, and DataDetail have some info which in Data object are null, so I can get from DataDetail and set in Data class .

`fetchData()
.toFlowable()
.flatMapIterable(datas->datas).map(new Function<Data, Flowable<DataDetail>>() {
@Override
 public Flowable<DataDetail>`apply(@io.reactivex.annotations.NonNull Data data) throws Exception {
return
  

fetchDataDetail(data()).toFlowable();
 }
}) .map(new Function<DataDetail, Flowable<Data>>() { 
@Override
 public Flowable<DataDetail> apply(@io.reactivex.annotations.NonNull DataDetail dataDetail) throws Exception {

return //I want to have access data and dataDetail object }
}) .toList();

.map() ? in map as argument I get DataDetail object which is Flowable, Flowable but I need to have access both Data and DataDetail to get some data from DataDetail and set in Data object

Upvotes: 0

Views: 855

Answers (1)

benjosantony
benjosantony

Reputation: 537

I believe that this is what your api is returning.

api.getList() // this returns a Single<List<Data>>

If that is correct may be this is what you are trying to achieve.

Single<List<Pair<Data,Detail>>> dataSingle = api.getList()
    .toFlowable()
    .flatMap(Flowable::fromIterable) // Convert the list to single emissions
    .flatMap(data -> {
            // Make api call to get details 
            return api.getDetails(data.getId())
                .flatMap(dataDetails -> {
                    return Flowable.just(Pair.create(data,dataDetail));
                });
        }).toList();

And obviously if you need to convert that to Flowable , you can easily do that with toFlowable.

The Pair is from android.support.v4.util.Pair. Ideally you create a separate model to represent the data from data and detail instead. But as a quick hack you can use Pair

Hope it helps.

Upvotes: 1

Related Questions