Filtering data using RXJava2 Flowable

六月ゝ 毕业季﹏ 提交于 2019-12-22 09:29:28

问题


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


回答1:


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())


来源:https://stackoverflow.com/questions/47226561/filtering-data-using-rxjava2-flowable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!