0
是否可以重新訂閱Observable並獲取錯誤?獲取錯誤並重新訂閱
Observable<T> retry()
方法重新訂閱了observable,但它消耗了錯誤。
final PublishSubject<Integer> observable = PublishSubject.create();
observable
.flatMap(new Func1<Integer, Observable<Integer>>() {
@Override
public Observable<Integer> call(final Integer integer) {
if (integer % 2 == 0) {
return Observable.just(integer);
} else {
return Observable.error(new Exception("int: " + integer));
}
}
})
.retry()
.subscribe(new Action1<Integer>() {
@Override
public void call(final Integer integer) {
Timber.i("integer: %d", integer);
}
},
new Action1<Throwable>() {
@Override
public void call(final Throwable throwable) {
Timber.e(throwable, "throwable");
}
},
new Action0() {
@Override
public void call() {
Timber.w("onCompleted");
}
});
Observable
.range(0, 10)
.delay(2, TimeUnit.MILLISECONDS)
.subscribe(new Action1<Integer>() {
@Override
public void call(final Integer integer) {
observable.onNext(integer);
}
},
new Action1<Throwable>() {
@Override
public void call(final Throwable throwable) {
observable.onError(throwable);
}
},
new Action0() {
@Override
public void call() {
observable.onCompleted();
}
});
的observable
的onError
部分不會被調用,因爲.retry()
消耗的錯誤。