rxjava: Can I use retry() but with delay?

后端 未结 14 2260
别那么骄傲
别那么骄傲 2020-11-28 18:25

I am using rxjava in my Android app to handle network requests asynchronously. Now I would like to retry a failed network request only after a certain time has passed.

14条回答
  •  感情败类
    2020-11-28 19:14

    in the event when you need to print out the retry count, you can use the example provided in Rxjava's wiki page https://github.com/ReactiveX/RxJava/wiki/Error-Handling-Operators

    observable.retryWhen(errors ->
        // Count and increment the number of errors.
        errors.map(error -> 1).scan((i, j) -> i + j)  
           .doOnNext(errorCount -> System.out.println(" -> query errors #: " + errorCount))
           // Limit the maximum number of retries.
           .takeWhile(errorCount -> errorCount < retryCounts)   
           // Signal resubscribe event after some delay.
           .flatMapSingle(errorCount -> Single.timer(errorCount, TimeUnit.SECONDS));
    

提交回复
热议问题