4

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);
}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Figen Güngör
  • 12,169
  • 14
  • 66
  • 108
  • What exactly do you want to do when an error happens? – OneCricketeer Dec 20 '16 at 22:47
  • Just neglect it and move on. It's autocomplete. For example, when there is no network connection, onError is called and observable stops. I want it to emit when network connection came back. – Figen Güngör Dec 20 '16 at 22:52
  • 1
    I'm not too well versed in RxJava, but sounds like a `retryWhen`. https://github.com/ReactiveX/RxJava/wiki/Error-Handling-Operators – OneCricketeer Dec 20 '16 at 22:56
  • Oh, yeah! I tried retry operator earlier but expected some onError logs and thought it wasn't working. It just doesnt get to onError part. When I turned on/off wifi, it was working as intended. Thanks, @cricket_007 – Figen Güngör Dec 20 '16 at 23:10
  • 1
    Have you tried onErrorResumeNext()? – Varundroid Dec 21 '16 at 00:52
  • Hmm, what should I write inside onErrorResumeNext, it takes a Func or Observable but what do I do with that to move on(neglect and keep listening) in case of error? Btw, I applied retry and it seems to be working. Why do you suggest that I should use onErrorResumeNext() instead of retry()? – Figen Güngör Dec 21 '16 at 13:56

2 Answers2

2

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.

Tassos Bassoukos
  • 16,017
  • 2
  • 36
  • 40
0

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.

Mihae Kheel
  • 2,441
  • 3
  • 14
  • 38