RxJava2 使用 retryWhen 和过滤器
RxJava2 using retryWhen with filter
我一直在研究 retryWhen() 方法,我注意到如果你在 filter() 中使用 filter() =]retryWhen() 并且如果 filter() 失败,则不会执行任何回调,甚至 onCompleted()。你能给我解释一下为什么会这样吗?提前致谢。
工作案例:
Observable.error(new RuntimeException())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.retryWhen(errors -> errors
.filter(throwable -> throwable instanceof RuntimeException)
.zipWith(Observable.range(1, 3), (throwable, retryCount) -> {
Log.i("lol", "retry " + retryCount);
return retryCount;
}))
.subscribe(e -> Log.i("lol", "onNext"), throwable -> Log.i("lol", "onError"), () -> Log.i("lol", "onCompleted"));
工作输出:
I: retry 1
I: retry 2
I: retry 3
I: onCompleted
但是当我用 filter(throwable -> throwable instanceof IOException)
更改过滤器时,可观察对象就像处于冻结状态。没有触发回调。
Observable.error(new RuntimeException())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.retryWhen(errors -> errors
.filter(throwable -> throwable instanceof IOException)
.zipWith(Observable.range(1, 3), (throwable, retryCount) -> {
Log.i("lol", "retry " + retryCount);
return retryCount;
}))
.subscribe(e -> Log.i("lol", "onNext"), throwable -> Log.i("lol", "onError"), () -> Log.i("lol", "onCompleted"));
您不想在 retryWhen()
运算符中使用 filter()
。相反,请使用 if
语句或 switch
语句来确保您完全涵盖所有情况。
retryWhen()
的工作方式是创建一个可观察对象并用它调用函数。当它在其 onError()
方法中捕获 throwable 时,它会将 throwable 发射到 observable 中并等待结果。如果它没有得到结果,比如当一个 throwable 被过滤时,它将永远等待。
我一直在研究 retryWhen() 方法,我注意到如果你在 filter() 中使用 filter() =]retryWhen() 并且如果 filter() 失败,则不会执行任何回调,甚至 onCompleted()。你能给我解释一下为什么会这样吗?提前致谢。
工作案例:
Observable.error(new RuntimeException())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.retryWhen(errors -> errors
.filter(throwable -> throwable instanceof RuntimeException)
.zipWith(Observable.range(1, 3), (throwable, retryCount) -> {
Log.i("lol", "retry " + retryCount);
return retryCount;
}))
.subscribe(e -> Log.i("lol", "onNext"), throwable -> Log.i("lol", "onError"), () -> Log.i("lol", "onCompleted"));
工作输出:
I: retry 1
I: retry 2
I: retry 3
I: onCompleted
但是当我用 filter(throwable -> throwable instanceof IOException)
更改过滤器时,可观察对象就像处于冻结状态。没有触发回调。
Observable.error(new RuntimeException())
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.retryWhen(errors -> errors
.filter(throwable -> throwable instanceof IOException)
.zipWith(Observable.range(1, 3), (throwable, retryCount) -> {
Log.i("lol", "retry " + retryCount);
return retryCount;
}))
.subscribe(e -> Log.i("lol", "onNext"), throwable -> Log.i("lol", "onError"), () -> Log.i("lol", "onCompleted"));
您不想在 retryWhen()
运算符中使用 filter()
。相反,请使用 if
语句或 switch
语句来确保您完全涵盖所有情况。
retryWhen()
的工作方式是创建一个可观察对象并用它调用函数。当它在其 onError()
方法中捕获 throwable 时,它会将 throwable 发射到 observable 中并等待结果。如果它没有得到结果,比如当一个 throwable 被过滤时,它将永远等待。