1
Observable<Integer> call = Observable.create(emitter -> {
    emitter.onNext(1);
    emitter.onError(new Throwable("Error"));
});

call
.retryWhen(throwableObservable -> throwableObservable)
.subscribe(integer -> System.out.println(integer), throwable -> System.out.println(throwable.getMessage()));

This code is working as expected, (constantly printing '1'), but if I change it to:

.retryWhen(throwableObservable -> throwableObservable.delay(1, TimeUnit.SECONDS))

It will only print '1' once and not retry.

GuilhE
  • 11,591
  • 16
  • 75
  • 116
Serge
  • 143
  • 1
  • 7
  • You introduced asynchrony in the delay process. Are you waiting long enough to see the retry? [Recommended reading](https://github.com/ReactiveX/RxJava#schedulers). – akarnokd Nov 21 '19 at 12:57
  • It should print '1' every second (with the .delay(1, TimeUnit.SECONDS) included), but it prints only once and the retry does not get triggered. – Serge Nov 21 '19 at 14:09
  • 1
    Works for me: https://gist.github.com/akarnokd/acff7ab937838467d6b8da90d1974900 – akarnokd Nov 21 '19 at 14:57
  • ok understood now and working, so the first option is just faster and it's not needed to pass on to the main thread, with delay, the main thread gains focus and stops the program, correct?. – Serge Nov 21 '19 at 15:08
  • 1
    The sleep prevents the main thread from premature exit which stops RxJava's schedulers by default. If you have an android or swing application, there is no need to sleep. – akarnokd Nov 21 '19 at 15:35

0 Answers0