Reputation: 3440
Trying to add retry to an RxJava2 retrofit2 call in Kotlin, however, cannot figure out the right syntax.
Did some research and found that I need to use retryWhen using pattern like (for retrying 3 times):
val api: Single<Item> = ...;
api
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.retryWhen {errors ->
errors
.zipWith(Observable.range(1, 3), { index: Int -> {} })
.flatMap {
count: Int -> Observable.timer(5, TimeUnit.SECONDS)
}
}
.subscribe { }
But with this I get various build errors:
None of the following functions can be called with the arguments supplied:
@CheckReturnValue @BackpressureSupport @SchedulerSupport public final fun <U : Any!, R : Any!> zipWith(p0: ((Subscriber<in (???..???)>!) -> Unit)!, p1: ((Throwable, ???) -> ???)!): Flowable<(???..???)>! defined in io.reactivex.Flowable
@CheckReturnValue @BackpressureSupport @SchedulerSupport public final fun <U : Any!, R : Any!> zipWith(p0: (Mutable)Iterable<(???..???)>!, p1: ((Throwable, ???) -> ???)!): Flowable<(???..???)>! defined in io.reactivex.Flowable
@CheckReturnValue @BackpressureSupport @SchedulerSupport public final fun <U : Any!, R : Any!> zipWith(p0: (Mutable)Iterable<(???..???)>!, p1: BiFunction<in Throwable!, in (???..???), out (???..???)>!): Flowable<(???..???)>! defined in io.reactivex.Flowable
@CheckReturnValue @BackpressureSupport @SchedulerSupport public final fun <U : Any!, R : Any!> zipWith(p0: Publisher<out (???..???)>!, p1: BiFunction<in Throwable!, in (???..???), out (???..???)>!): Flowable<(???..???)>! defined in io.reactivex.Flowable
What I am missing or doing wrong? I am not able to find good tutorial that I can go through to understand this better?
Upvotes: 0
Views: 236
Reputation: 1167
You have to make a change in your zipWith
operator to use BiFunction
like this:
.zipWith(Observable.range(1, 3), BiFunction { error: Throwable, index: Int -> index })
As you can see in your build errors, you have to provide Throwable
as first parameter in BiFunction
.
Upvotes: 2