https://kotlinlang.org logo
#android
Title
# android
p

pollux-

07/28/2020, 7:44 PM
You have two tables in Room(Rx), let say A & B. You have to check in table A, if the value returned is valid then check table B and return the result. Else return error to the user. How can this be done using single stream in Rx. Any suggestions?
g

Guillermo Alcantara

07/28/2020, 8:07 PM
Not sure I understand the question. Could you
tableASingle.flatMap { tableBSingle }
?
p

psh

07/28/2020, 8:10 PM
I think there’s a small step in between,
Copy code
queryTableA.flatMap {
    if (invalid) {
        Single.error(...)
    } else {
        queryTableB
    }
}
👆 2
💯 2
p

pollux-

07/28/2020, 8:44 PM
If inside the flatMap .. there is no way to avoid this ?
IMG_20200728_124750.jpg
@Guillermo Alcantara
g

Guillermo Alcantara

07/28/2020, 8:51 PM
avoid what?
👍 1
p

pollux-

07/28/2020, 11:46 PM
There is an IF check inside the flatMap(). Can we avoid that . That's look ugly to me 😂
g

Guillermo Alcantara

07/29/2020, 3:32 PM
well you could map the first single and throw an exception if invalid, but that's worse imo
if you are into that, you could create an extension
filterWithError
that does that and can be reused for other streams. Where if predicate is true passes it downstream, otherwise errs.