Sometimes we need to get some data before do something else, each one can be from different sources, like 2 different web services.

Using Retrofit 1.9 and retrolambda

  • NewOrder is the object that we need to create from 2 data sources, both getted from an Observable<Address>.
class NewOrder {


public NewOrder(Address dropAddress, Address pickAddress){


The Retrofit service that consumes the object and creates the final Observable:

Observable<Order> create(@Body NewOrder id);
  • We will have 2 Observables to wait for result: pickObservable and dropObservable. (can be more! just add each Observable to combineLatest`)

  • Address.get(...) is a function that will return an observable with the address for that LatLng.

  • Use Observable.combineLatest to combine both pickObservable and dropObservable and wait for it´s results and will end in a new Observable<NewOrder>.

  • Use flatMap to apply

public static Observable<Order> create(final OrderData orderData) {
// Prerequisites
Observable<Address> pickObservable = Address.get(new LatLng(1.2. -1,2));
Observable<Address> dropObservable = Address.get(new LatLng(1.2. -1,2);

// Create a new Observer that will end until both observables get data.
Observable<NewOrder> newOrderObservable =
Observable.combineLatest(pickObservable, dropObservable
(pickAddress, dropAddress,) ->
new NewOrder(pickAddress, dropAddress)

// create new observable from last observable.
return newOrderObservable.flatMap(order -> create(order));

private static Observable<Order> create(OrderService.NewOrder newOrder) {
return SERVICE.createOrder(newOrder);

Thanks to lukaciko. Check this question on stackoverflow.

Blog Logo

Daniel Gomez Rico




The findings and tips records of an Android-iOS-TheWholeShabang group

Back to Overview