I am writing an app that receive calls from JNI to static methods in a Java Class.
By example
public class Receiver {
// method called from C++ in some thread
private static void receive(int value) {
EventBus.instance().post(new ReceiverEvent(value));
}
}
And I want to listen for such event as part of my Observable object like this.
Observer.create(new Observable.Subscriber<String>() {
@Override
public void call(Subscriber<? super String> subscriber) {
// blocked until onEvent was fired
return subscriber.onNext("ok");
}
public void onEvent(ReceiverEvent receiverEvent) {
// fire call observable function
}
}
Any insights?
Thanks.
Technically, yes the observable needs to register itself with the EventBus, that's why it's never receiving the ReceiverEvent. But registering an anonymous class with the event bus is messy at best. The bigger problem is with your architecture.
RxJava, EventBus and Observable's are all tools that solve the same problem. They all implement more or less the same trick - the observable pattern. The difference is that EventBus and RxJava provide more features - mostly around the elegancy of the code - they make the code more readable and therefore faster to implement.
So in your case, I would suggest choosing one - RxJava or EventBus. There is no need to use both.
At the moment - it looks like you are using the Receiver to listen for something in the c++ code, then using the EventBus to pass that message on to an RxJava Observable, who then passes it on to whatever is observing. Why not cut out one or more of the middlemen? Make the Receiver an Observable class, or remove the observable altogether and just listen for the ReceivedEvent in which ever part of your app really needs it...