Martin G.
Martin G.

Reputation: 35

RxJava 2.0 - how to combine Observable and Completable

Say there is interface containing methods:

Observable<Data> makeHttpCall(int param1, boolean param2);

Completable storeInDatabase(Data data);

Completable combinedCall(int param1, boolean param2);

What is the best way to implement the combinedCall method that would:

  1. fetch data from makeHttpCall
  2. store it using storeInDatabase
  3. return Completable that completes when storeInDatabase completes?

Seems that in RxJava 1.0 it was possible to do Completable.merge(Observable) but merge does not seem to accept Observable any more.

Upvotes: 3

Views: 3161

Answers (1)

Bartek Lipinski
Bartek Lipinski

Reputation: 31438

First of all I don't believe merge is a good fit for your needs, as the storeInDatabase has to be performed on the results of makeHttpCall instead of parallel to it.

This should work for you:

Completable combinedCall(int param1, boolean param2) {
    return makeHttpCall(param1, param2)
            .flatMapCompletable(new Function<Data, CompletableSource>() {
                @Override
                public CompletableSource apply(@NonNull Data d) throws Exception {
                    return storeInDatabase(d);
                }
            });
}

Upvotes: 6

Related Questions