kotlin,得到了“类型不匹配。必填:一次性?找到:单元“在subscribe()

时间:2018-02-14 15:38:42

标签: kotlin rx-java2 subscribe

编辑:

基于德米特里·伊克里亚诺夫的建议, 使用DisposableObserver会编译,但会导致崩溃

io.reactivex.exceptions.ProtocolViolationException: It is not allowed to 
subscribe with a(n) com.DataManager$theObserver$1 multiple times. Please 
create a fresh instance of com.DataManager$theObserver$1 and subscribe that 
to the target source instead.

subecribWith()的唯一代码,只调用一次

fun initSession() {
    if (mDisposable != null && mDisposable!!.isDisposed) {
        mDisposable!!.dispose()
    }

    mDisposable = RxBus.listen(DataEvent::class.java).subscribeWith(theObserver)  <=== crash at here
}

DisposableObserver是类的成员变量:

var theObserver: DisposableObserver<DataEvent> = object : DisposableObserver<DataEvent>() {
    override fun onComplete() {
        Log.e(TAG, "onComplete: All Done!")        }

    override fun onNext(t: DataEvent) {
        Log.e(TAG, "Next: " + t)
        onDataReady(t)        }

    override fun onError(e: Throwable) {
        Log.e(TAG, "onError: ")
    }
}

===

原始问题:

尝试在kotlin中使用RxJava subscribe(),得到错误“Type mismatch. Required: Disposable? Found: Unit”,不确定它意味着什么,有谁知道?

class DataEvent {}

使用RxBus

object RxBus {

private val publisher = PublishSubject.create<Any>()

fun publish(event: Any) {
    publisher.onNext(event)
}

// Listen should return an Observable and not the publisher
// Using ofType we filter only events that match that class type
fun <T> listen(eventType: Class<T>): Observable<T> = publisher.ofType(eventType)

}

当像这样打电话时,没关系:

mDisposable = RxBus.listen(DataEvent::class.java).subscribe({
        onDataReady(it)
    })

但是在使用已定义的RxBus.listen(DataEvent::class.java).subscribe(observer)实例调用observer时 它显示红色下划线:“类型不匹配。必填:一次性?发现:单位“

mDisposable = RxBus.listen(DataEvent::class.java).subscribe(observer)

观察员是:

var observer: Observer<DataEvent> = object : Observer<DataEvent> {
    override fun onSubscribe(d: Disposable) {
        Log.e(TAG, "onSubscribe: ")
    }

    override fun onNext(@NonNull t: DataEvent) {
        Log.e(TAG, "onNext: " + t)
        onDataReady(t)
    }

    override fun onError(e: Throwable) {
        Log.e(TAG, "onError: ")
    }

    override fun onComplete() {
        Log.e(TAG, "onComplete: All Done!")
     }
}

1 个答案:

答案 0 :(得分:2)

这是因为在RxJava 2.0方法subscribe(observer)已更改并且未返回任何内容。

  

与版本1.x的Observable不同,subscribe(Observer)不允许外部取消订阅,并且Observer实例应该公开此类功能。

您可以使用subscribeWith(observer) 例如:

val disposable = Observable.just("Hello world!")
                .delay(1, TimeUnit.SECONDS)
                .subscribeWith(object : DisposableObserver<String>() {
                    public override fun onStart() {
                        println("Start!")
                    }

                    fun onNext(t: Int?) {
                        println(t)
                    }

                    override fun onError(t: Throwable) {
                        t.printStackTrace()
                    }

                    override fun onComplete() {
                        println("Done!")
                    }
                })