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

462 Views Asked by At

I am using java and Kotlin in my project and I am using from EventBus but don't work in Kotlin because I am using RX between Java code and Kotlin instead EventBus but don't get any result in Kotlin class.

My code is like bellow:

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)

}

And I am using in Java class:

EventBusTest.publish(123);

And I get in Kotlin class:

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

But I can't get any result.

Bellow is my libraries:

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'
1

There are 1 best solutions below

0
On

You are using two different data types. You are trying to listen to Int where are when you are publishing, it becomes Integer. Instead, try EventBusTest.listen(Integer::class.java) Also, Int is a Kotlin Class derived from Number

You can also try something like this with your existing code. var someNumber = 123 EventBusTest.publish(someNumber); This will work if you listen to Int::class.java

I hope that the explanation is clear.