https://kotlinlang.org logo
#rx
Title
# rx
b

blakelee

08/16/2019, 9:20 PM
I have a Maybe that then does a switchIfEmpty. The switchIfEmpty is still firing even though the value isn’t empty, anyone see anything like this? I am able to evaluate the Maybe by using blockingGet and can see that I’m getting my items. I should also mention that it’s a Flowable.firstElement to get that Maybe. Not sure if that’s relevant. I am using () instead of {} for the switchIfEmpty so parts of it are working
a

alexsullivan114

08/18/2019, 2:07 PM
Could you paste the code?
switchIfEmpty
shouldn't fire if the
Maybe
completes with a value.
b

blakelee

08/19/2019, 3:25 PM
Here is where I think the issue is
Copy code
fun getThing(clientId: String): Maybe<SomeResult> {
        return dao.getSomeThing(clientId)
                .firstOrError()
                .flatMapMaybe {
                    it.firstOrNull { item -> item.end == null }
                            ?.let { Maybe.just(it) } ?: Maybe.empty()
                }
                .onErrorComplete() // db error, unlikely
                .subscribeOn(<http://schedulers.io|schedulers.io>())
    }
This calls the above and then has the switch
Copy code
fun isThing(): Single<Boolean> {
        return repo.getThing("some id")
                .map { false }
                .switchIfEmpty(
                        if (condition) {
                            doThing() // Returns Maybe<Boolean>
                        } else {
                            Maybe.just(true)
                        })
                .onErrorComplete()
                .defaultIfEmpty(true) // Handles db error
                .toSingle()
    }
The dao above IS returning the item that I need. What happens is it goes to the top function, but also seems to go to the switchIfEmpty at the same time. The top function is getting a Flowable from the db and then getting the first thing. Perhaps it’s the flowable causing issues
Hmmm… Error doesn’t even occur at the first bock
If I do
Copy code
Maybe.just(item)
    .map { false }
    .switchIfEmpty(doSomething())
It’s still hitting the switchIfEmpty
a

alexsullivan114

08/19/2019, 5:55 PM
What does your
dao
look like? Is it a room
dao
?
b

blakelee

08/19/2019, 10:56 PM
Got it figured out. The whole eager vs lazy of switchIfEmpty
a

alexsullivan114

08/20/2019, 3:52 PM
fwiw, that's the scenario everywhere. Unless your function call is made in in a closure it's happening whenever it's declared.
193 Views