Reputation: 1144
I have 2 Observables, which call 2 WS
So first I get the list of cars. Everything is ok. But I would to get the owner for each car and set it in the Car entity, like car.setOwner(owner)
and send the final list of cars, containing their owner.
Api.getCars()
.subscribe(new Action1<List<Car>>() {
@Override
public void call(List<Car> cars) {
// get the list of cars, but need to get their owner
});
Which is the best way to do this? (Moreover, without lambdas.)
Upvotes: 0
Views: 379
Reputation: 11058
You could utilize this flatMap
overload:
Api.getCars()
.flatMap(cars -> Observable.from(cars)) // flatten your list
.flatmap(car -> Api.getOwner(car), // request each owner
(car, owner) -> {
car.setOwner(owner); // assign owner to the car
return Observable.just(car);
})
.toList() // collect items into List
... // here is your Observable<List<Car>>
Upvotes: 2