Rx Kotlin重试发行时间

时间:2019-06-20 11:57:00

标签: android kotlin observable rx-java

我尝试使用.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))
                }
            )
    }

1 个答案:

答案 0 :(得分:0)

我认为有人正在寻找类似的东西。我想出了一个对我来说很好的解决方案。

.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)
                }
            }
    }
}