Rxjava tolist()未完成

Jem*_*ahi 2 java android reactive-programming rx-java

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

代码

        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);
                }
            });
Run Code Online (Sandbox Code Playgroud)

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

更新

@Query("SELECT * from location")
Flowable<List<Location>> loadAllLocations();
Run Code Online (Sandbox Code Playgroud)

mModel.getLocations()只是调用像上面的loadAllLocations从房间percistance存储

Tha*_*han 8

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

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

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