我的 RxJava 调用链有问题。 toList 无法正常工作。我猜想 toList() 需要一些东西来完成。这就是它被卡住的原因。但我不知道如何解决这个问题
The code
mModel.getLocations()
.flatMapIterable(new Function<List<Location>, List<Location>>(){
@Override
public List<Location> apply(final List<Location> locations) throws Exception {
return locations;
}
})
.filter(new Predicate<Location>() {
@Override
public boolean test(final Location location) throws Exception {
return location.searchExistInNameOrKeyWord(input);
}
})
.toList()
.map(new Function<List<Location>, List<Location>>() {
@Override
public List<Location> apply(List<Location> locations) throws Exception {
/** Doing some random work with the list and then returning */
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer<List<Location>>() {
@Override
public void accept(final List<Location> locations) throws Exception {
mView.setAdapterItems(locations);
}
});
也许在 RxJava 方面比我优秀得多的人可以指出我做错了什么。
Update
@Query("SELECT * from location")
Flowable<List<Location>> loadAllLocations();
The mModel.getLocations()
只需像上面那样从 Room 持久存储中调用加载 AlLocations
我以前从未使用过 Room,但根据这篇文章:https://medium.com/google-developers/room-rxjava-acb0cd4f3757 https://medium.com/google-developers/room-rxjava-acb0cd4f3757
现在,每次更新用户数据时,我们的 Flowable 对象都会
自动发出,允许您根据最新更新更新 UI
数据。仅当查询结果包含 at 时 Flowable 才会发出
至少一排。当没有数据与查询匹配时,Flowable
不会发出,onNext 和 onError 都不会。
So, Flowable
对每个数据变化做出反应,这意味着onComplete
永远不会被调用。在这种情况下你不能使用toList()
运算符,因为该流永远不会完成。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)