使用RxJava2的Kotlin中的指数退避

使用RxJava2的Kotlin中的指数退避,kotlin,rx-java2,Kotlin,Rx Java2,我正在尝试对请求进行指数级重试,这样,如果请求失败(即,您的互联网已关闭),应用程序将无休止地重试,直到其正常工作(应用程序在前台的时间长度) 我试过了 公共类RetryWithDelay实现函数>{ 私有最终整数最大重试次数; 私人最终整数retryDelayMillis; 私人内部检索计数; 公共RetryWithDelay(最终整数最大重试次数,最终整数retryDelayMillis){ this.maxRetries=maxRetries; this.retryDelayMillis=

我正在尝试对请求进行指数级重试,这样,如果请求失败(即,您的互联网已关闭),应用程序将无休止地重试,直到其正常工作(应用程序在前台的时间长度)

我试过了

公共类RetryWithDelay实现函数>{
私有最终整数最大重试次数;
私人最终整数retryDelayMillis;
私人内部检索计数;
公共RetryWithDelay(最终整数最大重试次数,最终整数retryDelayMillis){
this.maxRetries=maxRetries;
this.retryDelayMillis=retryDelayMillis;
this.retryCount=0;
}
@凌驾
公共可观察应用(最终可观察>(){
@凌驾
公众可观察应用(最终可丢弃){
如果(++retryCount

但当我尝试将其转换为kotlin时,它表示函数只接受一个泛型参数。

我将Java代码复制粘贴到IntelliJ中,它为我完成了一半的工作:

import java.util.concurrent.TimeUnit
import io.reactivex.functions.Function
import io.reactivex.*

class RetryWithDelay(private val maxRetries: Int, private val retryDelayMillis: Long) : Function<Observable<Throwable>, Observable<Long>> {

    override fun apply(attempts: Observable<Throwable>): Observable<Long> {
        return attempts
                .flatMap(object : Function<Throwable, Observable<Long>> {

                    private var retryCount: Int = 0

                    override fun apply(throwable: Throwable): Observable<Long> {
                        return if (++retryCount < maxRetries) {
                            // When this Observable calls onNext, the original
                            // Observable will be retried (i.e. re-subscribed).
                            Observable.timer(retryDelayMillis,
                                    TimeUnit.MILLISECONDS)
                        } else Observable.error<Long>(throwable)

                        // Max retries hit. Just pass the error along.
                    }
                })
    }
}
import java.util.concurrent.TimeUnit
导入io.reactivex.functions.Function
导入io.reactivex*
类RetryWithDelay(private val maxRetries:Int,private val retryDelayMillis:Long):函数{
覆盖乐趣应用(尝试:可观察):可观察{
回击
.flatMap(对象:函数{
私有变量retryCount:Int=0
覆盖乐趣应用(可丢弃:可丢弃):可观察{
如果(++retryCount

请注意,retryCount已移动到内部平面图中,因此它不会在多个
观察者之间共享。

我将Java代码复制粘贴到IntelliJ中,它为我完成了一半的工作:

import java.util.concurrent.TimeUnit
import io.reactivex.functions.Function
import io.reactivex.*

class RetryWithDelay(private val maxRetries: Int, private val retryDelayMillis: Long) : Function<Observable<Throwable>, Observable<Long>> {

    override fun apply(attempts: Observable<Throwable>): Observable<Long> {
        return attempts
                .flatMap(object : Function<Throwable, Observable<Long>> {

                    private var retryCount: Int = 0

                    override fun apply(throwable: Throwable): Observable<Long> {
                        return if (++retryCount < maxRetries) {
                            // When this Observable calls onNext, the original
                            // Observable will be retried (i.e. re-subscribed).
                            Observable.timer(retryDelayMillis,
                                    TimeUnit.MILLISECONDS)
                        } else Observable.error<Long>(throwable)

                        // Max retries hit. Just pass the error along.
                    }
                })
    }
}
import java.util.concurrent.TimeUnit
导入io.reactivex.functions.Function
导入io.reactivex*
类RetryWithDelay(private val maxRetries:Int,private val retryDelayMillis:Long):函数{
覆盖乐趣应用(尝试:可观察):可观察{
回击
.flatMap(对象:函数{
私有变量retryCount:Int=0
覆盖乐趣应用(可丢弃:可丢弃):可观察{
如果(++retryCount

请注意,retryCount已移动到内部平面图中,因此它不会在多个
观察者之间共享。

Kotlin没有已检查的异常。无论如何,您不应该音译Java代码。请确保您导入了
io.reactivex.functions.Function
。Kotlin具有SAM转换,因此您应该能够只说
flatMap{throwable->…您的逻辑…}
对象的扩展情况如何?Kotlin没有检查异常。无论如何,你不应该音译Java代码。请确保你导入了
io.reactivex.functions.Function
。Kotlin有SAM转换,所以你应该能够只说
flatMap{throwable->…你的逻辑…}
对象的扩展情况如何?你是对的@akarnokd,Android Studio导入了错误的
函数
你是对的@akarnokd,Android Studio导入了错误的
函数