Chain Completable into Observable flow
Asked Answered
P

2

18

Suppose you want to insert a Completable in your Observable chain, such as for each emitted element, there is a completable that runs and blocks until it completes, what option would you choose? (here the Completable.complete() is just to make an example)

  1. .flatMap { Completable.complete().andThen(Observable.just(it)) }

  2. .doOnNext { Completable.complete().blockingAwait() }

  3. something else?

Phenosafranine answered 28/11, 2017 at 14:50 Comment(0)
P
26
.flatMapCompletable { Completable.complete().andThen(Observable.just(it)) } // If you don't want it to return
.flatMap { Completable.complete().andThen(Observable.just(it)) } //Can be used if you want it to return Observable
Prosody answered 14/12, 2017 at 4:23 Comment(0)
G
4

In option 2. you lose the capability of cancelling the completable because blockingAwait() is not managed by the observable flow.

If you don't need to return the emitted element, there is also flatMapCompletable.

If you need to execute the completable but also return the emitted element, then I would go with option 1.

Gui answered 28/11, 2017 at 15:11 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.