dev.farmer
dev.farmer

Reputation: 2779

Why doest Completable.andThen is not work?

I am developing an Android App using Kotlin, RxJava(RxKotlin).

I need to combine two Completables.

interface RetrofitApi {
    @Get
    fun getA(): Completable

    @Post
    fun save(obj: Any): Completable
}

I have a Retrofit code like upper.

And I tryed to chain them in one stream like:


fun getAndSave() {
    retrofitApi.getA()
                .andThen {
                    retrofitApi.save(any())
                }
                .subscribe()
}

But the second Completable is not run!

When I changed upper code like below code, it works fine!

fun getAndSave() {
    val secondCall = retrofitApi.save(any())
    retrofitApi.getA()
                .andThen(secondCall)
                .subscribe()
}

What difference???

Upvotes: 0

Views: 287

Answers (1)

Aarjav
Aarjav

Reputation: 1374

They are calling two different overloads of the andThen method.

In the first one, the save Completable is only created after the getA Completable completes. However it is never subscribed to. (a lambda is passed in which is ran when getA Completable completes)

In the second case (with the secondCall variable), the save Completable is created 'right away' but not subscribed to until getA Completable completes. (a CompletableSource is passed in which is subscribed to once getA Completable completes)

Upvotes: 1

Related Questions