然而,尝试在 Kotlin 中为 RxJava2 retrofit2 调用添加重试,无法找出正确的语法。
做了一些研究,发现我需要使用重试当使用类似模式时(重试 3 次(:
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 { }
但是有了这个,我得到了各种构建错误:
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
我错过了什么或做错了什么?我找不到好的教程,我可以通过更好地理解这一点?
您必须在zipWith
运算符中进行更改才能使用如下所示的BiFunction
:
.zipWith(Observable.range(1, 3), BiFunction { error: Throwable, index: Int -> index })
正如您在构建错误中看到的那样,您必须提供Throwable
作为BiFunction
中的第一个参数。