When the user types into the SearchView widget, the app should make an
API call (in background thread) to fetch search results from server, an
I use such an approach on a production app.
RxSearchView.queryTextChanges(searchView)
.debounce(300, TimeUnit.MILLISECONDS)
.filter(new Predicate() {
@Override
public boolean test(String text) throws Exception {
if (text.isEmpty()) {
return false;
} else {
return true;
}
}
})
.distinctUntilChanged()
.switchMap(new Function>() {
@Override
public ObservableSource apply(String query) throws Exception {
return dataFromNetwork(query);
}
})
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Consumer() {
@Override
public void accept(String result) throws Exception {
textViewResult.setText(result);
}
});