gpt4 book ai didi

android - Rx Kotlin retryWhen 问题

转载 作者:行者123 更新时间:2023-11-29 02:20:09 26 4
gpt4 key购买 nike

我正在尝试使用 .retryWhen() 操作在失败的情况下每 5 秒重试 3 次 api 调用。有人可以帮我处理这个案子吗,因为过去几个小时我都弄不明白。

.retryWhen { errors ->
errors
.zipWith(Observable.range(1, 3), { _: Throwable, i: Int -> i })
.flatMap { retryCount: Int ->
Observable.timer(
5.0.pow(retryCount.toDouble()).toLong(),
TimeUnit.SECONDS
)
}
}

我得到了

None of the following functions can be called with the arguments supplied:
@CheckReturnValue @SchedulerSupport public final fun <U : Any!, R : Any!> zipWith(p0: ((Observer<in Int!>) -> Unit)!, p1: ((Throwable, Int) -> ???)!): Observable<(???..???)>! defined in io.reactivex.Observable
@CheckReturnValue @SchedulerSupport public final fun <U : Any!, R : Any!> zipWith(p0: ObservableSource<out Int!>!, p1: BiFunction<in Throwable!, in Int!, out (???..???)>!): Observable<(???..???)>! defined in io.reactivex.Observable
@CheckReturnValue @SchedulerSupport public final fun <U : Any!, R : Any!> zipWith(p0: (Mutable)Iterable<(???..???)>!, p1: BiFunction<in Throwable!, in (???..???), out (???..???)>!): Observable<(???..???)>! defined in io.reactivex.Observable
@CheckReturnValue @SchedulerSupport public final fun <U : Any!, R : Any!> zipWith(p0: (Mutable)Iterable<Int!>!, p1: ((Throwable, Int) -> ???)!): Observable<(???..???)>! defined in io.reactivex.Observable

我还粘贴了我尝试执行的整个代码:

private fun getOrderDetails(uuid: String) {
apiClient.getOrderDetailsUsingUUID(uuid)
.retryWhen { errors ->
errors
.zipWith(Observable.range(1, 3), { _: Throwable, i: Int -> i })
.flatMap { retryCount: Int ->
Observable.timer(
5.0.pow(retryCount.toDouble()).toLong(),
TimeUnit.SECONDS
)
}
}
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe({ webOrderRequest ->
_currentOrder.value = OrderState.OrderDetails(webOrderRequest.order!!)
orderStatus(uuid, webOrderRequest.order)
},
{ error ->
_currentOrder.value = OrderState.Error(getErrorMessage(error))
Log.e(TAG, getErrorMessage(error))
}
)
}

最佳答案

我认为有人正在寻找类似的东西。我想出了一个适合我的解决方案。

.retryWhen { errors ->
errors.zipWith(Observable.range(1, 3), BiFunction { throwable: Throwable, count: Int -> Pair(throwable, count) })
.flatMap { count: Pair<Throwable, Int> ->
if (count.second < 3) {
Observable.timer(5, TimeUnit.SECONDS)
} else {
Observable.error(count.first)
}
}
}

它的扩展名:

import io.reactivex.Observable
import io.reactivex.functions.BiFunction
import java.util.concurrent.TimeUnit

fun <T> Observable<T>.retryWhenError(retryCount: Int, delayInSeconds: Long): Observable<T> {
return retryWhen { errors ->
errors.zipWith(
Observable.range(1, retryCount), BiFunction { throwable: Throwable, count: Int -> Pair(throwable, count) })
.flatMap { count: Pair<Throwable, Int> ->
if (count.second < retryCount) {
Observable.timer(delayInSeconds, TimeUnit.SECONDS)
} else {
Observable.error(count.first)
}
}
}
}

关于android - Rx Kotlin retryWhen 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56685471/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com