RxJava retryWhen 重试 returns 一个可完成的整个方法
RxJava retryWhen retry the whole method that returns a completable
每次 API 响应无效令牌错误(重新验证)时,我都会尝试重新验证令牌。我有这个小例子,它复制了我面临的问题。基本上第一次调用会抛出异常,会触发重试,重试的时候,auth方法没有完全调用一遍(不打印"Entered Auth",而是打印"authing...")。
public class Example {
AtomicInteger atom = new AtomicInteger(1);
public Example(){}
public void start(){
auth().andThen(call())
.retryWhen(throwableFlowable -> throwableFlowable.flatMap(throwable -> {
System.out.println("Retrying...\n");
return Flowable.timer(1, TimeUnit.SECONDS);
}))
.subscribe(integer -> System.out.println("Result: " + integer), e -> System.out.println("Error" + e.getMessage()));
}
public Completable auth(){
System.out.println("Entered Auth");
return Completable.create(emitter -> {
System.out.println("authing...");
emitter.onComplete();
});
}
public Single<String> call(){
return getId()
.flatMap(this::getNameById);
}
public Single<Integer> getId(){
return Single.create(emitter -> {
emitter.onSuccess(atom.getAndIncrement());
});
}
public Single<String> getNameById(int id){
return Single.create(emitter -> {
HashMap<Integer, String> hash = new HashMap<>();
hash.put(1, "s");
hash.put(2, "b");
if(id == 1){
emitter.onError(new Throwable());
}else{
emitter.onSuccess(hash.get(id));
}
});
}
}
同样,这是我的输出:
Entered Auth
authing...
Retrying...
authing...
Result: b
如何在重试时强制整个 auth() 方法 运行?
使用 Completable.defer
,它将包装您的 Completable 创建并在重试时重做它,而不仅仅是重新订阅。
Completable.defer(() -> auth()).andThen(call())
.retryWhen(throwableFlowable -> throwableFlowable.flatMap(throwable -> {
System.out.println("Retrying...\n");
return Flowable.timer(1, TimeUnit.SECONDS);
}))
.subscribe(integer -> System.out.println("Result: " + integer), e -> System.out.println("Error" + e.getMessage()));
输出:
Entered Auth
authing...
Retrying...
Entered Auth
authing...
Result: b
每次 API 响应无效令牌错误(重新验证)时,我都会尝试重新验证令牌。我有这个小例子,它复制了我面临的问题。基本上第一次调用会抛出异常,会触发重试,重试的时候,auth方法没有完全调用一遍(不打印"Entered Auth",而是打印"authing...")。
public class Example {
AtomicInteger atom = new AtomicInteger(1);
public Example(){}
public void start(){
auth().andThen(call())
.retryWhen(throwableFlowable -> throwableFlowable.flatMap(throwable -> {
System.out.println("Retrying...\n");
return Flowable.timer(1, TimeUnit.SECONDS);
}))
.subscribe(integer -> System.out.println("Result: " + integer), e -> System.out.println("Error" + e.getMessage()));
}
public Completable auth(){
System.out.println("Entered Auth");
return Completable.create(emitter -> {
System.out.println("authing...");
emitter.onComplete();
});
}
public Single<String> call(){
return getId()
.flatMap(this::getNameById);
}
public Single<Integer> getId(){
return Single.create(emitter -> {
emitter.onSuccess(atom.getAndIncrement());
});
}
public Single<String> getNameById(int id){
return Single.create(emitter -> {
HashMap<Integer, String> hash = new HashMap<>();
hash.put(1, "s");
hash.put(2, "b");
if(id == 1){
emitter.onError(new Throwable());
}else{
emitter.onSuccess(hash.get(id));
}
});
}
}
同样,这是我的输出:
Entered Auth
authing...
Retrying...
authing...
Result: b
如何在重试时强制整个 auth() 方法 运行?
使用 Completable.defer
,它将包装您的 Completable 创建并在重试时重做它,而不仅仅是重新订阅。
Completable.defer(() -> auth()).andThen(call())
.retryWhen(throwableFlowable -> throwableFlowable.flatMap(throwable -> {
System.out.println("Retrying...\n");
return Flowable.timer(1, TimeUnit.SECONDS);
}))
.subscribe(integer -> System.out.println("Result: " + integer), e -> System.out.println("Error" + e.getMessage()));
输出:
Entered Auth
authing...
Retrying...
Entered Auth
authing...
Result: b