Share
Embed
GreenRobot-Eventbus
Introduction of EventBus 3.0.0 and its advance features
Himanshu Dudhat
August 23, 2016
Transcript
24 slides
-
What is Eventbus? • An open-source library for Android using
the publisher/subscriber pattern for loose coupling. • Enables central communication to decoupled classes with just a few lines of code – simplifying the code, removing dependencies, and speeding up app development -
How EventBus works? Publisher Event Bus Subscriber Subscriber Event Event
Event post() onEvent() onEvent() -
How to use Eventbus? • Add dependency • Define Events
• Define Subscriber • Post Event -
Add Dependency EventBus is available on JCenter and Maven Central,
so just add this dependency to your Gradle script: Current version is : 3.0.0 compile 'org.greenrobot:eventbus:x.x.x' -
Define Events Events are POJO/Model/Bean without any specific requirements. e.g.
public class MessageEvent { public final String message; public MessageEvent(String message) { this.message = message; } } -
Define Subscribers // This method will be called when a
SomeOtherEvent is posted @Subscribe public void handleSomethingElse(SomeOtherEvent event) { doSomethingWith(event); } // This method will be called when a MessageEvent is posted (in the UI thread for Toast) @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(MessageEvent event) { Toast.makeText(getActivity(), event.message, Toast.LENGTH_SHORT).show(); } -
Continue… Register Eventbus Unregister Eventbus @Override public void onStart() {
super.onStart(); EventBus.getDefault().register(this); } @Override public void onStop() { EventBus.getDefault().unregister(this); super.onStop(); } -
Advanced features • Sticky Events • Delivery Threads (ThreadMode) •
Priorities and Event Cancellation • Configuration -
Sticky Events • EventBus keeps the last sticky event of
a certain type in memory when event posted sticky. • Used to post data even after subscriber unregistered and subscriber registered again • Used to cache data. E.g. sensor data, location data • Sticky event can be delivered to subscribers or queried explicitly. -
Continue… Way to post sticky event: Consume Sticky Event: EventBus.getDefault().postSticky(new
MessageEvent("Hello everyone!")); @Subscribe(sticky = true, threadMode = ThreadMode.MAIN) public void onEvent(MessageEvent event) { // UI updates must run on MainThread textField.setText(event.message); } -
Getting Sticky event manually: Getting Sticky event manually: Continue… MessageEvent
stickyEvent = EventBus.getDefault().getStickyEvent(MessageEvent.class); // Better check that an event was actually posted before if(stickyEvent != null) { // "Consume" the sticky event EventBus.getDefault().removeStickyEvent(stickyEvent); // Now do something with it } MessageEvent stickyEvent = EventBus.getDefault().removeStickyEvent(MessageEvent.class); // Better check that an event was actually posted before if(stickyEvent != null) { // Now do something with it } -
Delivery Threads (ThreadMode) • ThreadMode: POSTING • ThreadMode: MAIN •
ThreadMode: BACKGROUND • ThreadMode: ASYNC // Called in the same thread (default) // ThreadMode is optional here @Subscribe(threadMode = ThreadMode.POSTING); // Called in Android UI's main thread @Subscribe(threadMode = ThreadMode.MAIN); // Called in the background thread @Subscribe(threadMode = ThreadMode.BACKGROUND); // Called in a separate thread @Subscribe(threadMode = ThreadMode.ASYNC); -
Priorities Within the same delivery thread (ThreadMode), higher priority subscribers
will receive events before others with a lower priority. Default priority is 0. In different delivery thread priority of delivery will not affect. @Subscribe(priority = 1); public void onEvent(MessageEvent event) { … } -
Event Cancellation Events are usually cancelled by higher priority subscribers
Cancelling is restricted to event handling methods running in posting thread ThreadMode.POST (Default). EventBus.getDefault().cancelEventDelivery(event) ; -
Configuration EventBusBuilder is used to configure Eventbus Configure the default
EventBus instance EventBus eventBus = EventBus.builder().logNoSubscriberMessages(false) .sendNoSubscriberEvent(false).build(); EventBus.builder().throwSubscriberException(BuildConfig.DEBUG) .installDefaultEventBus(); eventInheritance(boolean eventInheritance) executorService(ExecutorService executorService) logNoSubscriberMessages(boolean logNoSubMsg) logSubscriberExceptions(boolean logSubscriberExcp) sendNoSubscriberEvent(boolean sendNoSubEvent) sendSubscriberExceptionEvent(Boolean send) throwSubscriberException(boolean throw) -
EventBus Features (Summary) • Convenient Annotation based API: Simply put
the @Subscribe annotation to your subscriber methods • Thread delivery support: Main(UI), Background, Async • Event & Subscriber inheritance : The OOPs apply to event and subscriber • Zero configuration: Immediately using a default EventBus instance • Configurable: Adjust its behavior using the builder pattern. -
Pros • Simplifies the communication • Decouples event senders and
receivers • Performs well with Activities, Fragments, and background threads • Avoids complex and error-prone dependencies and life cycle issues • Is fast; specifically optimized for high performance -
Continue… • Is tiny (<50k jar) • Has advanced features
like delivery threads, subscriber priorities, etc. -
Cons • Makes code unreadable and untestable. • You loss
control over who’s notifying who unless you use external plugins -
Thanks! +HimanshuDudhat @HimanshuDudhat [email protected] Team Lead - Android @ Cygnet
Infotech Pvt. Ltd. Sr. Software Engineer @ Cygnet Infotech Pvt. Ltd Himanshu Dudhat