1

How does Otto handle different event classes? Is it possible to have different event classes?

Would only the listeners that listen to the specific event class get notified? E.g. would the sample below work, with only the listener in class A being notified? Assume that EventClassA and EventClassB does not extend the same superclass.

class A {
    @Subscribe
    public void handleEvent(EventClassA event)
    {
            //
    }
}

class B {
    @Subscribe
    public void handleEvent(EventClassB event)
    {
            //
    }
}

class C {
    public void postEvent() {
        bus.post(new EventClassA());
    }
}
tsorn
  • 3,365
  • 1
  • 29
  • 48

1 Answers1

5

Is it possible to have different event classes?

Yes.

Would only the listeners that listen to the specific event class get notified?

Yes. In your sample, an instance of A that is subscribed to the bus will be called with handleEvent(), while an instance of B that is subscribed to the bus will not be called.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Just what I needed to know, thanks. Saves me a lot of trouble going through `instanceOf` etc. Would it be OK to have `static Bus bus = new Bus();` and a `Bus getBus()` method in the main class, and use this bus everywhere else? – tsorn Sep 21 '15 at 12:37
  • I don't actually know about the static bus but I would rather create separate buses for each event. The reason being that a static bus would mean that the class must exist for the static bus to be called, Also i think it would be easier for GC purposes if everytime you want to send the event, you create a new bus. – Simon Sep 21 '15 at 15:47
  • Yes, when using static bus I get `Event bus [Bus "default"] accessed from non-main thread null`. So if I create a new Bus every time I want to post something, it will still be received by other buses on other threads? – tsorn Sep 21 '15 at 15:56
  • @tsorn: "Would it be OK to have static Bus bus = new Bus(); and a Bus getBus() method in the main class, and use this bus everywhere else?" -- AFAIK, that is the recommended pattern. "when using static bus I get Event bus [Bus "default"] accessed from non-main thread null" -- that has nothing to do with it being static and everything to do with Otto thread enforcement, AFAIK. Personally, I use greenrobot's EventBus, as it has a more flexible threading model. "So if I create a new Bus every time I want to post something, it will still be received by other buses on other threads?" -- no. – CommonsWare Sep 21 '15 at 15:59
  • @CommonsWare: Yeah by changing to `private static Bus bus = new Bus(ThreadEnforcer.ANY);` it seems the error has disappeared. – tsorn Sep 21 '15 at 16:16
  • @tsorn: Right. The default enforcer is `MAIN`, which means you can only post from the main application thread. `ANY` allows you to post from any thread, though you still only receive on the main application thread. – CommonsWare Sep 21 '15 at 16:19
  • @CommonsWare: Is possible to post *and* receive from any thread somehow? I don't think I need it in my real application, but I ran into problems when unit testing, it seems that the unit tests run on another thread than the main application thread. – tsorn Sep 21 '15 at 18:22
  • 1
    @tsorn: A key Otto limitation is that the delivery thread is always the main application thread. That's why I use greenrobot's EventBus. It feels Otto-ish (because they both forked from Guava's EventBus), but you can specify, on a per-event-handling-method basis, what thread to use to receive the event on. – CommonsWare Sep 21 '15 at 18:28