I'm using debounce()
for handling user searching and dealing with it's pause on typing (searches 1 second after the last char):
RxSearchView.queryTextChanges(searchView)
.debounce(1, TimeUnit.SECONDS)
.subscribe(new Action1<CharSequence>() {
@Override
public void call(CharSequence charSequence) {
presenter.loadUsers(charSequence.toString());
}
});
so if user delete all chars, it waits 1 second and then loads the list, how can I handle it and load the list instantly?
In your case just debounce
operator with different parameters is needed:
public final <U> Observable<T> debounce(Func1<? super T, ? extends Observable<U>> debounceSelector)
Using it you can filter, which events can be delayed or not:
RxSearchView.queryTextChanges(searchView)
.debounce(new Func1<CharSequence, Observable<CharSequence>>() {
@Override
public Observable<CharSequence> call(CharSequence charSequence) {
if (charSequence.length() == 0) {
return Observable.empty();
} else {
return Observable.<CharSequence>empty().delay(1, TimeUnit.SECONDS);
}
}
})
.subscribe(new Action1<CharSequence>() {
@Override
public void call(CharSequence charSequence) {
Log.d(MainActivity.class.getSimpleName(), new Date().toGMTString() + " " + charSequence.length() + " :" + charSequence);
}
});
In the simplest form just merge the debounced observable with a manual trigger like so:
RxSearchView.queryTextChanges(searchView)
.debounce(1, TimeUnit.SECONDS)
.mergeWith(Observable.just("")) // manually tigger onNext with empty search
.subscribe(new Action1<CharSequence>() {
@Override
public void call(CharSequence charSequence) {
presenter.loadUsers(charSequence.toString());
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With