Skip to content

Instantly share code, notes, and snippets.

@antonshilov
Created April 25, 2019 09:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save antonshilov/c5701fa664fdd11bef25a39556250c18 to your computer and use it in GitHub Desktop.
Save antonshilov/c5701fa664fdd11bef25a39556250c18 to your computer and use it in GitHub Desktop.
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