Created
April 25, 2019 09:44
-
-
Save antonshilov/c5701fa664fdd11bef25a39556250c18 to your computer and use it in GitHub Desktop.
RxJava 2 extension function to do something and retry after error
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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