android – Chaining requests in Retrofit + RxJava-ThrowExceptions

Exception or error:

I have 2 APIs that I want to make request to in sequence and store their data in SQLite.

First I want to make request to API A and store its data in SQL table a. Then make request to API B and store its data in table b and some data in table a_b. The data stored in a_b is from request B alone.

How can I do this using RxJava. I read somewhere about using flatMap for this, something like this

    // store in DB here? How? maybe use map()?
    .flatMap(modelA -> {
        // or maybe store modelA in DB here?
        return apiService.B().map(modelB -> {
            storeInDB()l // store B here ?
            return modelB;

If I wasn’t using lambda functions, this would look as ugly as normal nested calls. Is this a better way to do it?

How to solve:

I don’t think using map operator is the best way to go with things like storing the result of the api call.

What I like to do is to separate those things inside doOnNext operators. So your example would be something like this:

        .doOnNext(modelA ->
        .flatMap(modelA -> apiService.B())
        .doOnNext(modelB ->;

(add necessary observeOn and subscribeOn yourself, exactly like you need them)


Yes, you can use flatmap for this exact purpose. See the below example (Assuming your service A returns Observable<FooA> and service B returns Observable<FooB>)

        .flatMap(new Func1<FooA, Observable<FooB>>() {
            public Observable<FooB> call(FooA fooA) {
                // code to save data from service A to db

                // call service B
                return api.serviceB();
         .subscribe(new Subscriber<FooB>() {
             public void onCompleted() {

             public void onError(Throwable e) {


             public void onNext(FooB fooB) {
                 // code to save data from service B to db


Leave a Reply

Your email address will not be published. Required fields are marked *