Android Message delivery based on Rxjava implementation of a Eventbus-rxbus (iv)

Source: Internet
Author: User

Objective:

The previous article learned that the emergence of the Android event bus Management Open source framework Eventbus,eventbus has greatly reduced development costs and development difficulties, today we use the current rounds Rxjava to achieve similar Eventbus event bus management, Now a lot of people are saying that this way to replace Eventbus, today we from the development of efficiency, the development of difficulty and other dimensions to analyze the end can replace Eventbus? Let's review what is Eventbus? See this article Android Messaging Eventbus 3.0 use details (iii)

Demand:

Although software engineers are afraid to hear the word "demand", but all functions come from demand, we want to implement a PUBLISH/SUBSCRIBE message bus management model based on Rxjava, which assumes you already know how to use Rxjava. Let's see how it's implemented.

Rxbus implementation process: 1.) add Rxjava, rxandroid references in Build.gradle
  Compile ' io.reactivex:rxandroid:1.1.0 '  compile ' io.reactivex:rxjava:1.1.2 '
2.) Declare a single case that manages the event bus
 Public classRxbus {PrivateConcurrenthashmap<object, list<subject>> subjectmapper =NewConcurrenthashmap<>(); Private Static volatileRxbus instance; PrivateRxbus () {} Public StaticRxbus getinstance () {Rxbus Inst=instance; if(Inst = =NULL) {            synchronized(Rxbus.class) {Inst=instance; if(Inst = =NULL) {Inst=NewRxbus (); Instance=Inst; }            }        }        returnInst; }}

Considering that the Rxbus single case is likely to have multiple threads of concurrent access, this storage event bus uses Concurrenthashmap

3.) Subscriber implementation Registration/DE-registration method

Registration/interpretation is marked with a tag, where the event class is tag to implement an event object that can correspond to multiple subscribers.

  /*** Register * *@paramTag *@return     */     Public<T> observable<t> Register (@NonNull class<t>tag) {List<Subject> subjectlist =subjectmapper.get (tag); if(NULL==subjectlist) {Subjectlist=NewArraylist<>();        Subjectmapper.put (tag, subjectlist); } Subject<t, t> subject =publishsubject.create ();        Subjectlist.add (subject); returnsubject; }    /*** Cancel Registration * *@paramTag*/     Public<T>voidUnregister (@NonNull class<t>tag, @NonNull Observable Observable) {List<Subject> subjects =subjectmapper.get (tag); if(NULL!=subjects)            {subjects.remove (observable); if(Subjects.isempty ()) {subjectmapper.remove (tag); }        }    }
4.) Implement send message, clear all registration

Sending an event message is done by getting a list of subscribers under the current event and then passing the event through a loop. As to which thread the event runs on, it is up to the subscriber to decide.

 /*** Send Message * *@paramEvent*/     Public<T>voidPost (@NonNull Object event) {List<Subject> subjectlist =Subjectmapper.get (Event.getclass ()); if(Subjectlist! =NULL&&!Subjectlist.isempty ()) {             for(Subject subject:subjectlist) {Subject.onnext (event); }        }    }    /*** Clear Subscription*/     Public voidClear () {if(Subjectmapper.isempty ()) {return;    } subjectmapper.clear (); }
5.) How to use

Event subscribers to register and clear all registrations, subscribers do not subscribe to a type of event to return a observable object, which is also a personal feel more headache, it is difficult to implement a subscriber corresponding to multiple event types, In addition, these observable objects are also required to be released one by one when the solution is registered.

    PrivateObservable<datasynevent>observable; Private voidregisterobservable () {observable= Rxbus.getinstance (). Register (datasynevent.class); Observable.observeon (Androidschedulers.mainthread ()). Subscribe (NewAction1<datasynevent>() {@Override Public voidCall (Datasynevent event) {LOG.E (TAG,"Event--2-->" +Event.getcount ());    }        }); }    Private voidunregisterobservable () {if(Observable! =NULL) {rxbus.getinstance (). Unregister (datasynevent.class, observable); }    }

Events are sent by the sender of the event, and the handling of the sending event is relatively friendly, similar to Eventbus.

  Rxbus.getinstance (). Post (new datasynevent ());
Rxbus, eventbus contrast:

1. From the introduction of dependency package comparison

Rxbus need to introduce two packages, Eventbus need to introduce a, if the project is not used in Rxjava programming, and can not reduce the dependency of the package.

2. Compare the difficulty of development

As mentioned above, the implementation of Rxbus is based on Rxjava, as a new programmatic functional programming, the requirements for developers are somewhat improved a little bit, and each subscription needs to return a observable, the subscriber to write the specific code to be executed in which thread, And Eventbus the latest version of the annotated pre-compilation method, the Subscriber registration can only call a function, and by annotating the way to indicate the priority of the event, receive the event to run on which thread, and the ability to implement sticky events, these rxbus need to be developed two times.

3.) Compare the efficiency of the development

Rxbus need to do a lot of two development, otherwise only simple event delivery, and Eventbus just a simple look at the API to get started. If a subscriber needs multiple observable global variables to register multiple events, it's not crazy, and Eventbus has implemented a subscriber subscription to multiple events, and one event for multiple subscribers.

4.) From the functional improvement of the comparison

The above three comparisons also show that Eventbus implements the priority of events, the thread scenarios in which the subscription event runs, and the sticky events, which need to be implemented two times above the Rxbus.

Summarize:

It is possible to implement simple event bus management based on Rxjava, but it is a bit too much to think about replacing Eventbus. So if there is no use of Rxjava in the project, or use Eventbus more reliable.

Android messaging based on Rxjava implementation of a Eventbus-rxbus (iv)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.