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'
You are using two different data types. You are trying to listen to
Int
where are when you are publishing, it becomesInteger
. Instead, tryEventBusTest.listen(Integer::class.java)
Also, Int is a Kotlin Class derived from NumberYou can also try something like this with your existing code.
var someNumber = 123 EventBusTest.publish(someNumber);
This will work if you listen toInt::class.java
I hope that the explanation is clear.