1

I know that if the Observable emits a piece of data, it resubscribes, and if the Observable emits an onError notification, it passes that notification to the observer and terminates. The problem is that if I emit a Obervable.just(1,2),but it won't be accept by the observer.So what's the usage of it?Dose it just tell it to resubscribes,and it's not important what data i emit?

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);
                        }
                    });

and the log is
retryWhen重试数据1
retryWhen重试数据1

so Observable.just(4,5) is gone?

王鹏宇
  • 31
  • 6

1 Answers1

1

You can check out this example from the documentation to better understand how the retryWhen supposed to work (source: 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);

Output is:

 subscribing
 delay retry by 1 second(s)
 subscribing
 delay retry by 2 second(s)
 subscribing
 delay retry by 3 second(s)
 subscribing
taygetos
  • 3,005
  • 2
  • 21
  • 29