Dinesh Ravi
Dinesh Ravi

Reputation: 1225

Is it good to replace broadcast sending and receiving the event states with GreenRobot's EventBus from Service to Activity?

This is a follow up from this post How to use GreenRobot's EventBus in broadcasting events from Service to Activity?

my use case revolves around a service and an Activity.

Service is used for tracking the changes in the BLE connection.

Activity is used for reporting that connection state to the UI.

Existing scenario. Service is using broadcast for sending the events (via sendBroadcast() method) of each state revolving around BLE (connected/Disconnected, Data available, etc..)

My Doubt: Can I make use of this GreenRobot's EventBus Library to control (send and receive events) in same way as broadcast does? If so, Is there anything I should consider (about thread safety) or to be must known, while replacing the broadcast control (send and receive) paradigm completely.

Upvotes: 0

Views: 206

Answers (1)

Moonbloom
Moonbloom

Reputation: 7918

I'm currently using Otto (very similar to EventBus) to accomplish what you're wanting to do.

I have a service which holds a timer, and shows a persistent notification. Each update of the notification also publishes the newest information for the Activity to receive and then update the UI.

It's ALOT easier to implement with Otto (and possibly also EventBus, i haven't used that particular library) as i needed to send 4 pieces of information each time i published some information, and it grew tiresome to add extras to intents each time with the regular sendBroadcast() system.

I haven't had any issues after switching to Otto, and it helped me clean up alot of my code.

One thing to note (with Otto atleast) is that it's setup by default to only allow publishing/subscribing on the main thread, and as you're using a service, you'll have to edit this. I'm not sure whether or not EventBus has the same, but look out for it in their documentation.

Upvotes: 3

Related Questions