I have a Maybe that then does a switchIfEmpty. The...
# rx
b
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
Could you paste the code?
switchIfEmpty
shouldn't fire if the
Maybe
completes with a value.
b
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
What does your
dao
look like? Is it a room
dao
?
b
Got it figured out. The whole eager vs lazy of switchIfEmpty
a
fwiw, that's the scenario everywhere. Unless your function call is made in in a closure it's happening whenever it's declared.
242 Views