Skip to content

Instantly share code, notes, and snippets.

@pamartineza
Last active August 31, 2020 10:19
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 pamartineza/6d4b6134b1909cd3e10628463f2bb71f to your computer and use it in GitHub Desktop.
Save pamartineza/6d4b6134b1909cd3e10628463f2bb71f to your computer and use it in GitHub Desktop.
//Our function Zips error emissions with a range to limit them to 3,
// and emits a 0L with a power of 2 based delay
webservice.getRequestSingle()
.retryWhen { errors: Flowable<Throwable> ->
errors.zipWith(
Flowable.range(1,3)
BiFunction<Throwable, Int, Int> { error: Throwable, retryCount: Int ->
retryCount
}
).flatMap { retryCount: Int ->
Flowable.timer(
Math.pow(2.toDouble(), retryCount.toDouble()).toLong(),
TimeUnit.SECONDS
)
}
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ data ->
//refreshScreenWithData
}, { error ->
//handle error
})
@fangzhzh
Copy link

fangzhzh commented Mar 28, 2019

Thanks for the article.
Small typoes:

Bifunction -> BiFunction
flapMap -> flatMap

@pamartineza
Copy link
Author

fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment