RetryWhen is giving some issues when using with Single

孤街醉人 提交于 2020-01-24 20:41:06

问题


I am trying to hit an API call using retrofit and receive the response from the call. I am using the Single of Rxjava to get the response. What I need to do is that a retry if the call fails. I have gone through lots of examples but it seems none could have been a help (Also because of my limited knowledge on RXjava and Kotlin).

Below is the function which does the call and the retryWhen function I wrote

 override fun fetchMessage(southDataModel: SouthDataModel): Single<ArrayList<SouthDataResponseModel>> {
    return retrofitService.getCTLNetworkService()
        .fetchMessage(fetchMessageMapper.transform(southDataModel))
        .map {
            southDataResponseMapper.transform(it)
        }
        .retryWhen { error ->
            error.flatMap {
                Flowable.timer(1, TimeUnit.SECONDS)
            }
        }
}

This is exactly what I want to achieve

  Single.timer(1, TimeUnit.SECONDS)
 *     .doOnSubscribe(s -&gt; System.out.println("subscribing"))
 *     .map(v -&gt; { throw new RuntimeException(); })
 *     .retryWhen(errors -&gt; {
 *         AtomicInteger counter = new AtomicInteger();
 *         return errors
 *                   .takeWhile(e -&gt; counter.getAndIncrement() != 3)
 *                   .flatMap(e -&gt; {
 *                       System.out.println("delay retry by " + counter.get() + " second(s)");
 *                       return Flowable.timer(counter.get(), TimeUnit.SECONDS);
 *                   });
 *     })
 *     .blockingGet();

Below is the error when I compile the code

Type mismatch: inferred type is (Throwable!) -> Flowable<Long!>! but ((Throwable!) -> Publisher!)! was expected

来源:https://stackoverflow.com/questions/59856216/retrywhen-is-giving-some-issues-when-using-with-single

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!