0

Can someone please mention - and explain if possible - the issues related to threads that I need to consider when using guava eventbus ? When I subscribe an android Activity and annotate one of its methods with (@Subscribe), and then post an event from another thread, I got an exception that the event didn't got dispatched.

(I know I need to be on the UI thread to update the UI, that's not what I'm talking about. The event isn't event dispatched !)

EDIT: Here is an example :

post:(This runs in a networking thread)

eventBus.post(new EventShowToast("According to alarm \'" + alarm.getName() + "\', profile \'" + profile.getName() + "\' is run."));

subscribe:(a method in an activity, the activity register itself in onResume(...) )

@Subscribe
    @AllowConcurrentEvents
    public void showToast(EventShowToast event) {
        showToast(event.getMsg());
    }
  • Please show an example of the `@Subscribe` method and how you're posting the event that's dispatched. Threading shouldn't have anything to do with whether an event is dispatched or not. – ColinD Apr 15 '15 at 19:02

2 Answers2

0

Are you registering the annotated class with the event bus.

eventBus.register(annotatedClassInstance);

Event Bus Explained

alenz316
  • 613
  • 3
  • 10
  • I am pretty sure the exception is thrown at eventBus.post(event); and not eventBus.register(instance); – alenz316 Apr 15 '15 at 19:10
  • Also be sure that the register and post are on the same EventBus instance. – alenz316 Apr 15 '15 at 19:11
  • ok, but I still have to know the reason ... they're on the same instance, it's in a singleton class ... – Yahia Mohammad Apr 15 '15 at 19:12
  • Can you post your stack trace that you are getting? – alenz316 Apr 15 '15 at 21:39
  • That's all what I get : 04-12 20:46:35.829 9971-11208/xxx.android.init E/default﹕ Could not dispatch event: xxx.android.presentation.control.MainActivity@21139768 to public void xxx.android.presentation.control.MainActivity.showToast(xxx.core.event.EventShowToast) – Yahia Mohammad Apr 16 '15 at 19:57
  • Sorry should have caught this sooner. I'll add another answer that is the correct solution. – alenz316 Apr 16 '15 at 21:01
0

You can only toast from a UI thread. EventBus catches all exceptions and then throws it's own "Could not dispatch event" exception, which is why it is hard to see what the true error was.

The solution to your problem can be found at Toast from a Non-UI thread

Community
  • 1
  • 1
alenz316
  • 613
  • 3
  • 10
  • Thank you, I think this is the reason. I'll make a change to the code and test it, then I'll be back. – Yahia Mohammad Apr 16 '15 at 21:11
  • Thank you @alenz316 . The problem is solved now. Eventbus being catching all exceptions and then just throwing that ambiguous message was very confusing. – Yahia Mohammad Apr 18 '15 at 09:28