2

I have an activity A and a service S. They commute via LocalBroadcastManager.

If S calls sendBroadcast twice with two messages M1 and M2 in order, will A get M1 before M2?

Thanks,

Joe C
  • 2,757
  • 2
  • 26
  • 46
  • 2
    BTW, if you want this sort of local messaging, but with more options, check out [greenrobot's EventBus](https://github.com/greenrobot/EventBus) and [Square's Otto](http://square.github.io/otto/). – CommonsWare Jan 25 '14 at 19:11
  • Which one has a better support and is used by more projects? – Joe C Jan 25 '14 at 19:42
  • They're close, though I suspect that Otto is used a bit more. Support for both is mostly just issue trackers and public resources like StackOverflow, AFAIK. – CommonsWare Jan 25 '14 at 19:45
  • I went through http://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? – Joe C Jan 25 '14 at 20:13
  • And https://github.com/greenrobot/EventBus does not mention whether its post is async or sync. – Joe C Jan 25 '14 at 20:15
  • 1
    Actually, it does, but it discusses it in the context of delivery threads: https://github.com/greenrobot/EventBus#delivery-threads – CommonsWare Jan 25 '14 at 20:43

2 Answers2

4

LocalBroadcastManager has two ways for you to broadcast; sendBroadcast() and sendBroadcastSync(). One is synchronous, and the other is asynchronous. sendBroadcastSync() blocks until the receiver for the first message is done running.

Emmanuel
  • 13,083
  • 4
  • 39
  • 53
  • 2
    IOW, with `sendBroadcastSync()`, you can be guaranteed order, because M1 will be delivered before you have a chance to send M2. With `sendBroadcast()`, I think the net effect will be that they are delivered in order, but that's not documented behavior and therefore is probably something you should not rely upon. – CommonsWare Jan 25 '14 at 19:08
  • How about the delay of sendBroadcastSync()? Intuitively, in my case it does not seem to be a very busy operation because I only use it to commute two components. Am I correct? – Joe C Jan 25 '14 at 19:39
  • It will depend on how much your receiver takes to finish. – Emmanuel Jan 25 '14 at 19:42
0

Not necessarily. This call deliver method is asynchronous, there is another way of sending ordered broadcasts (based on permissions, basically the one's that have more permissions get the broadcast earlier than the rest), sendOrderedBroadcast(...). You may find more info here.

nKn
  • 13,691
  • 9
  • 45
  • 62