使用Android Studio 3.0在Kotlin中使用RxJava zipWith IDE

前端之家收集整理的这篇文章主要介绍了使用Android Studio 3.0在Kotlin中使用RxJava zipWith IDE前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我想创建一个Observable,它从Observable中发出一些包含Interval Observable对象列表的项目,这样第一个observable中的项目就会有一些时间延迟.
这是我的实现:
val just1 = ArrayList<SomeClass1>()

填充一些项目

fun populateJust1() {
just1.add(SomeClass1("23",23))
just1.add(SomeClass1("24",24))
just1.add(SomeClass1("25",25))
}

populateJust1()

和区间Observable压缩

Observable.fromIterable(just1)
            .zipWith(Observable.interval(2,TimeUnit.SECONDS)) { item: SomeClass1,interval: Long -> item }
            .subscribe(Consumer<SomeClass1> { someClass1 -> Log.v("someClass1",someClass1.toString()) })

但是,IDE,Android Studio 3.0以红色方式强调了zipWith运算符,并说:

None of the following functions can be called with the arguments supplied.
zipWith(((observer: Observer) → Unit)!,((t1: SomeClass1,t2: Long) → R)!) where R cannot be inferred; U = Long for fun zipWith(other: ((observer: Observer) → Unit)!,zipper: ((t1: SomeClass1,t2: U) → R)!): Observable! defined in io.reactivex.Observable
zipWith(ObservableSource!,BiFunction!) where R cannot be inferred; U = Long! for fun zipWith(other: ObservableSource!,zipper: BiFunction!): Observable! defined in io.reactivex.Observable
zipWith((Mutable)Iterable!,BiFunction!) where U,R cannot be inferred for fun zipWith(other: (Mutable)Iterable!,t2: Long) → R)!) where R cannot be inferred; U = Long for fun zipWith(other: (Mutable)Iterable!,t2: U) → R)!): Observable! defined in io.reactivex.Observable

怎么了?我刚从Java类转换了这个代码,其中一切都是正确的

解决方法

Kotlin无法推断出zipWith lambda中的类型.

使用BiFunction而不是lambda:

data class SomeClass(val a: String,val b: Int)

val list = listOf(SomeClass("1",1),SomeClass("2",2))

Observable
        .fromIterable(list)
        .zipWith(Observable.interval(2,TimeUnit.SECONDS),BiFunction { item: SomeClass,_: Long -> item })
        .subscribe { Log.v("someClass",it.toString())
原文链接:https://www.f2er.com/android/313896.html

猜你在找的Android相关文章