0

I'm using RxBinding to handle search in my app. How to get the result as a List of the items? toList().toBlocking().single() is not an option inside infinite stream because onComplete() will never be called. Any ideas? Thanks.

RxSearchView.queryTextChanges(searchView)
                    .filter(queryString -> queryString.length() > 3 || queryString.length() == 0)
                    .debounce(500, TimeUnit.MILLISECONDS)
                    .map(CharSequence::toString)
                    .flatMap(s -> Observable.from(fishPlaces)
                                            .filter(fishPlace -> fishPlace.getName().toLowerCase().contains(s.toLowerCase())))
                    .subscribe(fishPlace -> { **// HOW TO GET A LIST<FISHPLACE> HERE???** }, throwable -> {});
lordmegamax
  • 2,684
  • 2
  • 26
  • 29

2 Answers2

1

The observable inside your flatmap can be made blocking though. So you could just map() instead of flatmap() and do the toList() on the observable of fishplaces.

.map(s -> Observable.from(fishPlaces).filter(fP-> foo(s, fP)).toList()))

Hendrik Marx
  • 709
  • 4
  • 9
1

What I guess you are after is the list of "fishPlaces" that matches a specific search string.

I also guess that the the amount of "fishPlaces" is finite, since it wouldn't make sense to make a list of it otherwise.

With minor changes (without actually running the code), I guess the following would work:

RxSearchView.queryTextChanges(searchView)
                    .filter(queryString -> queryString.length() > 3 || queryString.length() == 0)
                    .debounce(500, TimeUnit.MILLISECONDS)
                    .map(CharSequence::toString)
                    .map(s -> Observable.from(fishPlaces)
                                        .filter(fishPlace -> fishPlace.getName().toLowerCase().contains(s.toLowerCase()))
                                        .toList().toBlocking().single())
                    .subscribe(fishPlaces -> { /* Go fishing! */ }, throwable -> {});
Albin
  • 4,180
  • 2
  • 27
  • 19