Search code examples
rx-javareactive-programming

Elegant way to chain Completables


How to chain Completables in a more elegant way? .toSingleDefault(0) seems to be hacky

Single.just(newGroup)
.flatMapCompletable {
    dao.delete(currentGroup)
}
.toSingleDefault(0)
.flatMapCompletable {
     dao.insert(newGroup)
}

Solution

  • I tried before

    Single.just(newGroup)
        .flatMapCompletable {
            dao.delete(currentGroup)
        }
        .andThen {
            dao.insert(newGroup)
        }
    

    and it didn't work without explicit .subscribe(), but calling it in () like

    Single.just(newGroup)
        .flatMapCompletable {
            dao.delete(currentGroup)
        }
        .andThen (
            dao.insert(newGroup)
        )
    

    will subscribe it automatically