I have an EditText
where user inputs a search query and I want to perform an instant search on my server when user types something.
I try to do this with RxJava
as follows:
RxTextView.textChanges(editQuery) // I'm using RxBinding for listening to text changes
.flatMap(new Func1<CharSequence, Observable<UserPublic[]>>() {
@Override
public Observable<UserPublic[]> call(CharSequence query) {
return api.searchUsers(query); // I'm using Retrofit 1.9 for network calls. searchUsers returns an Observable<UserPublic[]>
}
})
.subscribe(Observers.create(
new Action1<UserPublic[]>() {
@Override
public void call(UserPublic[] userPublics) {
processResult(userPublics);
}
})
, new Action1<Throwable>() {
@Override
public void call(Throwable throwable) {
processError(throwable);
}
});
the problem is that if the network call encounters an error, the whole observable stops. So when user continues typing, nothing happens.
How can I modify this code so that:
- Whenever there is a network problem,
processError
is called - But when user continues typing, new network calls continue to be issued (leading to
processResult
/processError
again)