3

When RoboGuice fires Event, where will my event callback be executed, in which thread? For example, I have an activity which has do(@Observes OnUpdateUiEvent e). I also have a background thread which fires new OnUpdateUiEvent("data"). So, my do() method will be executed in bg thread as I understood? What will be, if I annotate do() with @Background from AndroidAnnotations? Should preprocessor make call to do() in runInUiThread()?

If everything is right, I think this pattern will provide the easiest way of communicating between threads.

Dennis Jaamann
  • 3,547
  • 2
  • 23
  • 42
naixx
  • 1,176
  • 11
  • 17

1 Answers1

6

As far as I can see here and there, you can specify the way threads should mix with events in RoboGuice, by using @Observes(EventThread.CURRENT), @Observes(EventThread.UI) or @Observes(EventThread.BACKGROUND).

The default is "CURRENT", which means that if you didn't specify anything, the event listening method will be executed in the same thread as the method receiving the event.

So yes, if you fire your event from a background thread, do() will be executed in a background thread.

If you add @Background on the do() method, then it will always be executed in a separate thread, different from the one where you fired the event.

If you're not sure, put a breakpoint and watch the thread names :-) .

Did that answer your question?

Pierre-Yves Ricau
  • 8,209
  • 2
  • 27
  • 43
  • Thanks, for your answer(saw your post in the group also). If I understood correctly, when we set BACKGROUND thread, application spawns new thread just for our callback? – naixx Dec 15 '11 at 17:43
  • Well, that's basically the idea, but not exactly. In fact, it uses a thread from the FixedThreadPool of 25 threads that is used in the SafeAsyncTask of RoboGuice. Here are the classes to look at to understand what happens : [EventListenerThreadingDecorator](http://code.google.com/p/roboguice/source/browse/roboguice/src/main/java/roboguice/event/eventListener/factory/EventListenerThreadingDecorator.java) then [AsynchronousEventListenerDecorator](http://code.google.com/p/roboguice/source/browse/roboguice/src/main/java/roboguice/event/eventListener/AsynchronousEventListenerDecorator.java) – Pierre-Yves Ricau Dec 28 '11 at 17:08
  • Then [RunnableAsyncTaskAdaptor](http://code.google.com/p/roboguice/source/browse/roboguice/src/main/java/roboguice/event/eventListener/RunnableAsyncTaskAdaptor.java) and [SafeAsyncTask](http://code.google.com/p/roboguice/source/browse/roboguice/src/main/java/roboguice/util/SafeAsyncTask.java) – Pierre-Yves Ricau Dec 28 '11 at 17:09
  • Thank you for your answer, it might help – naixx Dec 31 '11 at 10:02
  • @Piwaï Doesn't RoboGuice skip over an important detail ? that is un-registering from `EventManager` when objects are going away. I think this has to be a manual step and not done automatically. – S.D. Nov 27 '14 at 05:12