Chaining requests in Retrofit + RxJava

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:

apiService.A()
        .doOnNext(modelA -> db.store(modelA))
        .flatMap(modelA -> apiService.B())
        .doOnNext(modelB -> db.store(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>)

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

                // call service B
                return api.serviceB();
            }
         })
         .subscribeOn(Schedulers.io())
         .observeOn(AndroidSchedulers.mainThread())
         .subscribe(new Subscriber<FooB>() {
             @Override
             public void onCompleted() {
             }

             @Override
             public void onError(Throwable e) {

             }

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

             }
        });