Q:

How can I chain a list of Completable after executing another Completable

I am trying to save some data in a database. For adding methods I use Completable. After I save one entity - customer, I want to save a list of days, but only after customer is saved. This is my code now.     
mDatabaseManager.addCustomer(customer)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(() -> {
                    for (Day day : days) {
                        mDatabaseManager.addDay(day)
                                .subscribeOn(Schedulers.io())
                                .subscribe(() -> {});
                    }
                });

It works, but sometimes I get an error:
io.reactivex.exceptions.OnErrorNotImplementedException: FOREIGN KEY constraint failed (code 787)

And I also learned that this is not a proper way how to do it and I should chain the completable. I found concatWith and andThen operators, but I still can not rewrite this code. Could anyone show me how? 

A:

You could andThen the list of days wrapped into an Observable and use concatMapCompletable to insert them one after the other.
mDatabaseManager.addCustomer(customer)
            .subscribeOn(Schedulers.io())
            .andThen(
                 Observable.fromIterable(days)
                 .concatMapCompletable(day -> 
                      mDatabaseManager.addDay(day)
                      .subscribeOn(Schedulers.io())
                 )
            )
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe();

