Rxjava2EventBus
krishnachaitanya9984
2
Visit GitHub RepoEvent Buses

Rxjava2EventBus

Library for Event bus - implemented using RxJava2 in Android

Usage of the library :

  • Step 1 : Add following code in build.gradle (By default jcenter() is present in repositories)
repositories {
    jcenter()
}
  • Step 2 : Add the library in the dependencies section(build.gradle)
compile 'com.krinyny.reactive:rxjava2-eventbus:1.0.1'

As EventBus mechanism follows pub-sub pattern,there should be event publisher and subscriber for the event.

Following is the way to publish an event using this event bus.

EventBus.publishEvent(event);

"event" is of any class type(where data resides) so that subscriber can consume that event and perform action on top of it.

There are three modes of sending event.

Publish,Behaviour & Replay.

By Default, the event sent is of type "Publish". Different events can be sent in the following way

EventBus.publishEvent(event,EventBus.EventType.Behavior);

Following describes how to subscribe and consume an event:

EventBus.getFlowable(Event.class)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Event>() {
                    @Override
                    public void accept(@NonNull Event event) throws Exception {
                        //Do something with "event" object
                    }
                });

Different event processor can be defined as follows

EventBus.getFlowable(Event.class, EventBus.EventType.Behavior);
EventBus.getFlowable(Event.class, EventBus.EventType.Replay);

We can use different operators like map,flatmap,zip etc., while subscribing for an event.

We can also mention the thread on which it has to run & take action using subscribeOn & ObserveOn

In RxJava2 , to avoid back pressure Flowable is used.So using Flowable Processor we can send events.

Don't forget to dispose the Disposable after its usage

Disposable testDisposable = EventBus.getFlowable(Event.class).subscribe();

> use CompositeDisposible or dispose() method to dispose it.

To see it in action checkout the project and run the app.

Well, there is always room for improvements.Inbox me for suggestions @ krishnachaitanya9984@gmail.com

Become a better Android Developer
Millions of developers are learning at MindOrks

Online Training Program

Featured Blogs

Our Team

MindOrks is Certainly one of the best online blog to stay on top of all the Android development news, coding and design patterns. Finally a blog I can count on to keep myself updated with latest and greatest things happening in Android world.

Vipul Shah
Android Developer
Microsoft

Become Pro in Android by watching videos

OUR LEARNERS WORK AT