Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
RxJava 2 extension function to do something and retry after error
package com.antonshilov.rxextensions
import io.reactivex.Single
fun <T, R> Single<T>.doOnErrorAndRetry(action: Single<R>): Single<T> =
onErrorResumeNext {
it.printStackTrace()
action.flatMap { this }
}
fun main() {
var x = 0
val consumeX = Single.fromCallable {
if (x == 0) throw RuntimeException()
else "OK"
}
val changeX = Single.fromCallable {
x = 1
}
val res = consumeX
.doOnErrorAndRetry(changeX)
.blockingGet()
print(res)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment