Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rxjava tolist() not completing

I have a problem with my RxJava callchain. The toList is not working properly. I would guess that it is that the toList() needs something to complete. That is why it is stuck. But i do not know how to solve this issue

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

Maybe someone that is a lot better than me at RxJava could pinpoint what i am doing wrong.

Update

@Query("SELECT * from location")
Flowable<List<Location>> loadAllLocations();

The mModel.getLocations() just calls the loadAllLocations like above from the Room percistance storage

like image 518
Jemil Riahi Avatar asked Dec 19 '22 05:12

Jemil Riahi


1 Answers

I've never used Room before but according to this article: https://medium.com/google-developers/room-rxjava-acb0cd4f3757

Now, every time the user data is updated, our Flowable object will emit automatically, allowing you to update the UI based on the latest data. The Flowable will emit only when the query result contains at least a row. When there is no data to match the query, the Flowable will not emit, neither onNext nor onError.

So, Flowable reacts on every data change and that means onComplete would never be called. In that case you cannot use toList() operator because this stream will never complete.

like image 184
Than Avatar answered Jan 06 '23 18:01

Than