我知道,如果Observable发出一条数据,它将重新订阅,如果Observable发出onError通知,则它将该通知传递给观察者并终止。问题是,如果我发出一个Obervable.just(1,2),但它不会被观察者接受。那么它的用途是什么?剂量只是告诉它重新订阅,所以我不知道什么数据很重要发射?
Observable.just(1, "2", 3)
.cast(Integer.class)
.retryWhen(new Function<Observable<Throwable>, ObservableSource<Integer>>() {
@Override
public ObservableSource<Integer> apply(Observable<Throwable> throwableObservable) throws Exception {
return Observable.just(4,5);
}
})
.subscribe(new Consumer<Integer>() {
@Override
public void accept(Integer integer) throws Exception {
Log.i(TAG, "retryWhen重试数据"+integer);
}
});
日志是
retryWhen重试数据1
retryWhen重试数据1
那么Observable.just(4,5)不见了吗?
答案 0 :(得分:0)
您可以从文档中查看此示例,以更好地了解retryWhen应该如何工作(来源:http://reactivex.io/RxJava/javadoc/io/reactivex/Observable.html#retryWhen-io.reactivex.functions.Function-):
Observable.create((ObservableEmitter<? super String> s) -> {
System.out.println("subscribing");
s.onError(new RuntimeException("always fails"));
}).retryWhen(attempts -> {
return attempts.zipWith(Observable.range(1, 3), (n, i) -> i).flatMap(i -> {
System.out.println("delay retry by " + i + " second(s)");
return Observable.timer(i, TimeUnit.SECONDS);
});
}).blockingForEach(System.out::println);
输出为:
subscribing
delay retry by 1 second(s)
subscribing
delay retry by 2 second(s)
subscribing
delay retry by 3 second(s)
subscribing