I'm trying to accomplish the following: Return some data as single, execute a completable after. The following code does not compile due to single.andThen(). The actions need to be executed in this order.
val single = Single.create<String> { it.onSuccess("result") }
val completable = Completable.create { println("executing cleanup") }
val together = single.andThen(completable)
together.subscribe(
{ println(it) },
{ println(it) }
)
Use flatMap
:
single.flatMap(v -> completable.andThen(Single.just(v)))
Note that if you don't care whether the result is Single or Completable there is a special flatMapCompletable
operator in RxJava2 to execute completable after Single:
single.flatMapCompletable(result -> completable);
Assuming you actually want to return the Single after the Completable, here's another way:
Using Java:
single.flatMap(x -> completable.toSingleDefault(x))
Using Kotlin:
single.flatMap { completable.toSingleDefault(it) }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With