Jaume Colom Ferrer Jaume Colom Ferrer - 1 month ago 4x
Android Question

How to run 2 queries sequentially in a Android RxJava Observable?

I want to run 2 asynchronous tasks, one followed by the other (sequentially). I have read something about ZIP or Flat, but I didn't understand it very well...

My purpose is to load the data from a Local SQLite, and when it finishes, it calls the query to the server (remote).

Can someone suggests me, a way to achieve that?

This is the RxJava Observable skeleton that I am using (single task):

// RxJava Observable
Observable.OnSubscribe<Object> onSubscribe = subscriber -> {
try {

// Do the query or long task...

} catch (Exception e) {

// RxJava Observer
Subscriber<Object> subscriber = new Subscriber<Object>() {
public void onCompleted() {
// Handle the completion

public void onError(Throwable e) {
// Handle the error

public void onNext(Object result) {

// Handle the result




The operator to do that would be merge, see http://reactivex.io/documentation/operators/merge.html.

My approach would be to create two observables, let's say observableLocal and observableRemote, and merge the output:

Observable<Object> observableLocal = Observable.create(...)
Observable<Object> observableRemote = Observable.create(...)
Observable.merge(observableLocal, observableRemote)

If you want to make sure that remote is run after local, you can use concat.