Search code examples
kotlinrx-javarx-java2

RxJava Return single, execute completable after


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) }

)

Solution

  • Use flatMap:

    single.flatMap(v -> completable.andThen(Single.just(v)))