The performance of sync post in Otto (Event bus)

3.3k Views Asked by At

I used Otto for the event bus in my app. I went through square.github.io/otto. It said 'Posting to the bus is a synchronous action so when program execution continues it is guaranteed that all subscribers have been called.' I am curious why it does not provide async posting. Will performance be an issue here if we only have sync posting? Does it mean receivers of bus event should consider the performance issue by using other async tasks or threading to prevent from blocking everything?

Thanks.

2

There are 2 best solutions below

0
On

I am curious why it does not provide async posting

You would need to ask the authors of Otto that question. Anyone else will only be able to offer an opinion, and that's off-topic for StackOverflow.

Does it mean receivers of bus event should consider the performance issue by using other async tasks or threading to prevent from blocking everything?

It depends upon:

  1. What thread you are posting the event from (e.g., if it is posted from a background thread, there may be fewer issues)

  2. What work is supposed to be done in response to the event

This is not significantly different from anything else in Android: you need to know, as a developer, what thread you will be called on and how much time you plan to spend on that thread, in order to know whether you need to do something to make that work be asynchronous or not.

IOW, Otto addresses the issue of getting the events routed, but threading is still up to you. This is one area in which greenrobot's EventBus offers more options.

0
On

An intentional design decision was to avoid threading. Allowing to post from other threads will not allow Otto to have a predictable order of event deliveries. This adds more complexity to Otto, which contradicts one of main design decisions - simplicity of the library.

If you want, you can see a presentation of Eric Burke where he explains this (around 32:10).

There should be no performance issues as well. Just perform all your long-running tasks in a background thread, then post() update from the main thread back to Bus. There are planty of methods for doing this like Activity.runOnUiThread(), AsyncTask.onPostExecute() or Handler.post().

If you need more, you can use TinyBus, which uses same interfaces but it's way faster and it supports background precessing, or EventBus .