簡體   English   中英

Android - 我無法從 Kotlin 中的 RX 而不是 EventBus 獲得任何結果

[英]Android - I can't get any result from RX in Kotlin instead EventBus

我在我的項目中使用javaKotlin ,我從EventBus使用,但不在Kotlin工作,因為我在Java代碼和Kotlin之間使用RX而不是EventBus但在Kotlin class沒有得到任何結果。

我的代碼如下:

object EventBusTest {

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

    @JvmStatic
    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)

}

我在Java class

EventBusTest.publish(123);

我參加了Kotlin class

EventBusTest.listen(Int::class.java).subscribe{
    Log.i("LOG", "result:  $it")
}

但我無法得到任何結果。

波紋管是我的圖書館:

rxKotlin : "io.reactivex.rxjava2:rxkotlin:${versions.rxKotlin}",
....
implementation 'io.reactivex.rxjava2:rxjava:2.1.9'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'com.jakewharton.retrofit:retrofit2-rxjava2-adapter:1.0.0'
implementation 'com.netflix.rxjava:rxjava-android:0.20.3'

您正在使用兩種不同的數據類型。 您正在嘗試在發布時收聽Int where are ,它變為Integer 相反,請嘗試EventBusTest.listen(Integer::class.java)此外,Int 是從 Number 派生的 Kotlin 類

您也可以使用現有代碼嘗試類似的操作。 var someNumber = 123 EventBusTest.publish(someNumber); 如果您聽Int::class.java這將起作用

我希望解釋清楚。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM