Rxjava tolist()未完成

时间:2017-07-17 08:12:48

标签: java android rx-java reactive-programming

我的RxJava调用链出现问题。 toList无法正常工作。我猜这是toList()需要完成的东西。这就是它被卡住的原因。但我不知道如何解决这个问题

代码

command -v node

也许在RxJava上比我好多了的人可以找出我做错了什么。

更新

        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);
                }
            });

@Query("SELECT * from location") Flowable<List<Location>> loadAllLocations(); 只从Room percistance存储中调用上面的loadAllLocations

2 个答案:

答案 0 :(得分:8)

我之前从未使用过Room,但根据这篇文章:https://medium.com/google-developers/room-rxjava-acb0cd4f3757

  

现在,每次更新用户数据时,我们的Flowable对象都会   自动发光,允许您根据最新更新UI   数据。仅当查询结果包含at时,Flowable才会发出   至少一排。当没有数据与查询匹配时,Flowable   不会发出onNext和onError。

因此,Flowable会对每次数据更改做出反应,这意味着永远不会调用onComplete。在这种情况下,您无法使用toList()运算符,因为此流将永远不会完成。

答案 1 :(得分:0)

toList()运算符的关键点是它将等待发出所有项目。一旦完成它意味着onComplete()方法必须被调用,我们将得到我们理想的结果。如果我们没有使用onComplete()或由于某些原因onComplete()没有被调用,那么什么都不会打印。

相关问题