Search code examples
androidrx-javarx-java2rx-android

RxJava filter does not emit result to subscribe


I have RxJava filter to get POA's only that are not deleted(deactivated) and I am quite sure that I have one result, but I do not get any result in subscribe.

Disposable disposable = appDatabase.poaDao().getAllMine()
    .flatMap(poaDbs -> Flowable.fromIterable(poaDbs))
    .filter(poaDb -> !poaDb.isDeleted())
    .toList()
    .subscribeOn(Schedulers.io())
    .observeOn(AndroidSchedulers.mainThread())
    .subscribe(poaDbs ->
                   view.onActivePoasIssuedByMe(poaDbs),
               throwable -> view.handleError(throwable));

What could be the reason? I have tried debugging but it never gets to sunbscribe().


Solution

  • Your issue is that the Observable is not completing, so .toList() will never be executed.
    You said you need to filter the list you are getting, but if that is the case, you have a different option.

    Instead of doing this:

    .flatMap(poaDbs -> Flowable.fromIterable(poaDbs))
        .filter(poaDb -> !poaDb.isDeleted())
        .toList()
    

    You want to do:

    .flatMapSingle(poaDbs -> Observable.fromIterable(poaDbs)
         .filter(poaDb -> !poaDb.isDeleted())
         .toList())
    

    Note that the .filter and .toList operators are applied to the internal Observable.

    Extra note: this is even easier when using Kotlin since it provides the filter operation on collections, and you don't have to rely on RxJava or Java8 streams