Figen Güngör
Figen Güngör

Reputation: 12579

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

Upvotes: 4

Views: 2245

Answers (2)

Mihae Kheel
Mihae Kheel

Reputation: 2661

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.

Upvotes: 0

Tassos Bassoukos
Tassos Bassoukos

Reputation: 16152

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.

Upvotes: 2

Related Questions