Skip to content

Instantly share code, notes, and snippets.

@jsaund
Created February 23, 2017 21:11
Show Gist options
  • Save jsaund/d5b3f02ae3f6b331160c387f5472b059 to your computer and use it in GitHub Desktop.
Save jsaund/d5b3f02ae3f6b331160c387f5472b059 to your computer and use it in GitHub Desktop.
An example of using onErrorResumeNext to try to prevent the upstream from terminating when an error is received -- this doesn't work!
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 will terminate upstream (ie. we will stop receiving text view changes after an error!)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment