Search code examples
androidrx-javarx-binding

How to avoid observable stopping after onError


After onError, my observable stops working. How can I avoid that?

Here is my autocomplete observable and subscription code:

public void subscribeAutoComplete() {
    autoSubscription = RxTextView.textChangeEvents(clearableEditText)
            .skip(1)
            .map(textViewTextChangeEvent -> textViewTextChangeEvent.text().toString())
            .filter(s -> s.length() > 2)
            .debounce(400, TimeUnit.MILLISECONDS)
            .flatMap(text -> autoCompleteService.getAutoCompleteTerms(text)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread()))
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(new Subscriber<List<String>>() {
                @Override
                public void onCompleted() {
                    Log.d("rx", "oncomplete");
                }

                @Override
                public void onError(Throwable t) {
                    Log.e("rx", t.toString());
                }

                @Override
                public void onNext(List<String> strings) {

                    autoAdapter = new ArrayAdapter<>(MainActivity.this,
                                android.R.layout.simple_dropdown_item_1line, strings);
                    clearableEditText.setAdapter(autoAdapter);
                    clearableEditText.showDropDown();

                }
            });

    compositeSubscriptions.add(autoSubscription);
}

Solution

  • It's simple, just ignore the errors:

    autoCompleteService.getAutoCompleteTerms(text).onErrorResumeNext(Observable.empty())
    

    Note that this is potentially dangerous, as you'll ignore all errors; in this case it's probably OK, but be careful of overusing this.