我很难理解retryWhen
。
我的用例如下:
我该怎么做?
谢谢你!
更新
到目前为止我尝试了什么:
public class RetryTest {
private int i = 0;
@Test
public void testRetry() {
Observable
.create((Observable.OnSubscribe<String>) subscriber -> {
if (i == 0) {
i++;
throw new RuntimeException("Main error");
} else {
subscriber.onNext("Main success");
subscriber.onCompleted();
}
})
.retryWhen(observable -> {
// HERE IS WHERE I NEED HELP, i need something like:
Observable.just("Error handling observable")
.subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
// Do retry ONCE
}
@Override
public void onError(Throwable e) {
// Propagate error
}
@Override
public void onNext(String s) {
// Nothing to do here
}
});
})
.subscribe(new Subscriber<String>() {
@Override
public void onCompleted() {
System.out.println("onCompleted");
}
@Override
public void onError(Throwable e) {
System.out.println("onError: " + e);
}
@Override
public void onNext(String s) {
System.out.println("onNext: " + s);
}
});
}
}
答案 0 :(得分:0)
有更简单的解决方案。在您的情况下,您不需要retryWhen
运算符,请改为使用onErrorResumeNext
:
Observable mainObservable = Observable.just("Your main Observable");
Observable secondObservable = Observable.just("Your second Observable");
mainObservable
.onErrorResumeNext(o1 -> secondObservable.flatMap(o2 -> mainObservable))
.subscribe();
此处onErrorResumeNext
拦截错误并执行第二个Observable,然后执行主Observable。如果有错误
- 它将向下游传播。一切符合您的标准。