Skip to content

Instantly share code, notes, and snippets.

@jsaund
Created February 23, 2017 21:17
Show Gist options
  • Save jsaund/e0060c27c4d75b5d14675eb04711c787 to your computer and use it in GitHub Desktop.
Save jsaund/e0060c27c4d75b5d14675eb04711c787 to your computer and use it in GitHub Desktop.
Demonstrates using onErrorResumeNext to prevent terminating the upstream sequence. Notice that the onErrorResumeNext is applied inside the switchMap rather than outside.
public Observable<SearchResult> search(@NotNull EditText searchView) {
return RxTextView.textChanges(searchView) // In production, share this text view observable, don't create a new one each time
.map(CharSequence::toString)
.debounce(500, TimeUnit.MILLISECONDS) // Avoid getting spammed with key stroke changes
.filter(s -> s.length() > 1) // Only interested in queries of length greater than 1
.observeOn(workerScheduler) // Next set of operations will be network so switch to an IO Scheduler (or worker)
.switchMap(query -> searchService.query(query) // Take the latest observable from upstream and unsubscribe from any previous subscriptions
.onErrorResumeNext(Observable.empty()); // <-- This fixes the problem since the error is not seen by the upstream observable
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment