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.
For Kotlin & RxJava1 version
class RetryWithDelay(private val MAX_RETRIES: Int, private val DELAY_DURATION_IN_SECONDS: Long)
: Function1, Observable<*>> {
private val START_RETRY: Int = 1
override fun invoke(observable: Observable): Observable<*> {
return observable.delay(DELAY_DURATION_IN_SECONDS, TimeUnit.SECONDS)
.zipWith(Observable.range(START_RETRY, MAX_RETRIES),
object : Function2 {
override fun invoke(throwable: Throwable, attempt: Int): Int {
return attempt
}
})
}
}