How to avoid observable stopping after onError

2.1k Views Asked by At

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);
}
2

There are 2 best solutions below

3
Tassos Bassoukos On BEST ANSWER

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.

0
Mihae Kheel On

Using tryOnError works for me and it will call error inside subscribe() as well without getting UndeliverableException, app stop running or need of RxJavaPlugins.setErrorHandler which will make UI related more difficult to handle.