Android RxJava2重试用法未编译时

时间:2018-09-04 06:42:32

标签: android kotlin rx-java rx-java2

试图在Kotlin中向RxJava2 retrofit2调用添加重试,但是找不到正确的语法。

进行了一些研究,发现我需要使用retryWhen使用类似模式(用于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

我缺少什么或做错了什么?我找不到很好的教程,可以更好地理解它?

1 个答案:

答案 0 :(得分:2)

您必须在zipWith运算符中进行更改,才能像这样使用BiFunction

.zipWith(Observable.range(1, 3), BiFunction { error: Throwable, index: Int -> index })

正如您在构建错误中看到的那样,您必须在Throwable中提供BiFunction作为第一个参数。