使用RXJava2 Flowable过滤数据 [英] Filtering data using RXJava2 Flowable

查看:442
本文介绍了使用RXJava2 Flowable过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Room和RxJava,并且我想利用第二个功能来过滤来自第一个的数据.

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

然后,例如,我想按18岁以上的年龄过滤用户,因此我执行了以下操作:

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

不幸的是,这不起作用. 我的猜测是,toList()永远不会完成,因为onTerminated绝不会被房间调用. 所以我的问题是:我在做什么错?如何过滤用户,但最后仍然有Flowable?

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 ?

谢谢

推荐答案

房间将永远不会调用onComplete(然后toList将永远不会结束),但是使用fromIterable构建的内部流是有限的,并且会触发onComplete.因此,应该在flatMap内部的流上调用toList和toFlowable

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

这篇关于使用RXJava2 Flowable过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆