Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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);
}
like image 665
Figen Güngör Avatar asked Oct 22 '25 06:10

Figen Güngör


1 Answers

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.

like image 185
Tassos Bassoukos Avatar answered Oct 23 '25 22:10

Tassos Bassoukos



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!