How to subscribe to click events so exceptions don't unsubscribe?

I'm using RxJava for Android (RxAndroid) and I subscribe to click events of a view, and do something on them as follows:

subscription = ViewObservable.clicks(view, false) .map(...) .subscribe(subscriberA);

The problem is whenever there is an exception, subscriberA automatically unsubscribes, leading to the next click not triggering anything.

How to handle exceptions so to intercept all the click events regardless of exceptions being thrown?


Use retry method:

subscription = ViewObservable.clicks(view, false) .map(...) .retry() .subscribe(subscriberA)

However, you will not receive any exception in onError. To handle exceptions with retry (resubscribe) logic use retryWhen:

subscription = ViewObservable.clicks(view, false) .map(...) .retryWhen(new Func1<Observable<? extends Notification<?>>, Observable<?>>() { @Override public Observable<?> call(Notification errorNotification) { Throwable throwable = errorNotification.getThrowable(); if (errorNotification.isOnError() && handleError(throwable)) { // return the same observable to resubscribe return Observable.just(errorNotification); } // return unhandled error to handle it in onError and unsubscribe return Observable.error(throwable); } private boolean handleError(Throwable throwable) { // handle your errors // return true if error handled to retry, false otherwise return true; } } .subscribe(subscriberA)

人吐槽 人点赞



用户名: 密码:
验证码: 匿名发表


查看评论:How to subscribe to click events so exceptions don't unsubscribe?