Upgrade to Pro — share decks privately, control downloads, hide ads and more …

GreenRobot-Eventbus

 GreenRobot-Eventbus

Introduction of EventBus 3.0.0 and its advance features

Avatar for Himanshu Dudhat

Himanshu Dudhat

August 23, 2016
Tweet

More Decks by Himanshu Dudhat

Other Decks in Technology

Transcript

  1. 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
  2. How to use Eventbus? • Add dependency • Define Events

    • Define Subscriber • Post Event
  3. 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'
  4. 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; } }
  5. 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(); }
  6. 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(); }
  7. Advanced features • Sticky Events • Delivery Threads (ThreadMode) •

    Priorities and Event Cancellation • Configuration
  8. 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.
  9. 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); }
  10. 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 }
  11. 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);
  12. 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) { … }
  13. 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) ;
  14. 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)
  15. 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.
  16. 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
  17. Continue… • Is tiny (<50k jar) • Has advanced features

    like delivery threads, subscriber priorities, etc.
  18. Cons • Makes code unreadable and untestable. • You loss

    control over who’s notifying who unless you use external plugins
  19. Thanks! +HimanshuDudhat @HimanshuDudhat [email protected] Team Lead - Android @ Cygnet

    Infotech Pvt. Ltd. Sr. Software Engineer @ Cygnet Infotech Pvt. Ltd Himanshu Dudhat