Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RxJava2 How do I chain a Single to a Completable such that it get subscribed to when the Completable is complete

How do I chain a Single to a Completable such that it get subscribed to when the Completable is complete?

repository.downloadUser() is the Single.

Based on debugging it seems that the Single in this method gets called but never subscribed (i.e. the downloadUser() method gets called but no code inside the Single that it creates is called).

The question is, how do I get the repository.downloadUser() Single to be subscribed to in the chain using the original subscriber? What am I missing or doing wrong? Or is this not possible?

fun login(username: String, password: String): Completable {    
    return repository.login(username, password)
        .andThen {
            repository.downloadUser() // This is a Single
                .flatMap { downloadedUser ->
                    user = downloadedUser
                    it.toSingle()
                }
                // When I get this part working there are other things I want to 
                // chain as well.
}

Edit: I'll probably just break it up into two calls, one for the Completable, and then another one for the rest. However it would still be good to know if this is possible or not if anyone knows...

like image 781
Michael Vescovo Avatar asked Mar 06 '23 02:03

Michael Vescovo


1 Answers

Pretty sure this is your use of curly braces. Kotlin thinks you're invoking the RxJava method

public final Completable andThen(CompletableSource next) {
   return concatWith(next);
}

meaning the return type is Completable, but you want it as a Single. What you'll need is

repository.login(username, password)
    .andThen(repository.downloadUser())
    .flatmap { ... }
like image 128
Chris Horner Avatar answered Mar 09 '23 00:03

Chris Horner