Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filtering data using RXJava2 Flowable

I am using Room and RxJava and I would like to use the power of the second to filter data coming from the first.

Let's say room is returning Users.

Flowable<List<User> getUsers()

Then I wanted to filter users by age > 18 for example, so I performed the following :

userDao.getUsers()
.flatMap(listUser -> Flowable.fromIterable(listUser).filter(user -> user.age > 18))
.toList()
.toFlowable()

Unfortunately this is not working. My guess is that toList() is never finishing since onTerminated is never called by room. So my question is : what am I doing wrong ? How can I filter my users and still have a Flowable at the end ?

Thanks

like image 220
Mathieu H. Avatar asked Feb 13 '26 08:02

Mathieu H.


1 Answers

Room will never call onComplete (and then the toList will never finish) but the inner flow built using fromIterable is finite and will trigger onComplete. So the toList and toFlowable should be called on the flow inside the flatMap

userDao.getUsers()
.flatMap(listUser -> Flowable.fromIterable(listUser).filter(user -> user.age > 18).toList().toFlowable())
like image 93
Mathieu H. Avatar answered Feb 15 '26 00:02

Mathieu H.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!