我在使用 retryWhen
时遇到一些问题RxJava 中的函数。
当我创建一个要传递给 retryWhen
的函数时在 Observable
,我无法调用 zipWith
功能。看来 Kotlin 正在期待一些无法在这里转换的东西。
在此示例中,it.zipWith
不起作用(还有其他一些方法可用,但不是这个):
val retryFunc = Func1<Observable<out Throwable>, Observable<Any>> {
// zipWith is not possible to call
it.zipWith<Int, Any>(Observable.range(1, 3), Func2<Throwable, Int, Any> { throwable, integer ->
if (integer > 2) {
return@Func2 Observable.error<Any>(Exception())
}
Observable.timer(1, TimeUnit.SECONDS)
})
}
Observable.just("1", "2", "3").retryWhen(retryFunc)
如果我将传入参数改为 Func1<Observable<in Throwable> ...
,in
关键字使得可以使用zipWith
功能。但我改了之后,调用retryWhen(retryFunc)
显示错误:
Type mismatch: Expecting out Throwable, found in Throwable
val retryFunc = Func1<Observable<in Throwable>, Observable<Any>> {
it.zipWith<Int, Any>(Observable.range(1, 3), Func2<Throwable, Int, Any> { throwable, integer ->
if (integer > 2) {
return@Func2 Observable.error<Any>(Exception())
}
Observable.timer(1, TimeUnit.SECONDS)
})
}
Observable.just("1", "2", "3").retryWhen(retryFunc) // type mismatch here, expected out, found in
有谁知道我如何在 Kotlin 中接收和生成相同的类型?
能够创建zipWith
并返回期望值?
最佳答案
找到解决方案:
it.cast(Throwable::class.java).zipWith
或者这个:
(observable as Observable<Throwable>).zipWith
类型转换Throwable
解决了,希望Kotlin有别的办法(不依赖 Observable.cast
方法)。
关于rx-java - Kotlin通配符方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34538029/