Search code examples
androidkotlinrx-java2

RxJava's doOnSubscribe called after emit not before


I have a RxJava's chain that looks like this:

Completable.complete()
    .andThen(fetchData())
    .andThen(fetchAnotherData())
    .andThen(...)
    .doOnSubscribe {
        /* some action */
    }

The problem is that code in doOnSubscribe callback called after last andThen(). But I want it to be called before fetching any data. How do I achieve it?


Solution

  • Try defer the subscription of fetchData() Completable

       Completable.complete()
                .andThen(Completable.defer { fetchData() })
                .andThen(Completable.defer { fetchAnotherData() })
                .doOnSubscribe { /* some action */ }