"The fifth article" Androideventbus Source code reading and analysis of unregister code analysis

Source: Internet
Author: User

The code is written off Eventbus generally we will write in Ondestory:

    1. 1 eventbus.getdefault (). Unregister (this);
then go to unregister inside to see:
  1. 1 /**2      * @paramSubscriber3      */4      Public voidUnregister (Object subscriber) {5         if(Subscriber = =NULL) {6             return;7         }8         synchronized( This) {9 Mmethodhunter.removemethodsfrommap (subscriber);Ten         } One}


then go toMmethodhunter.Removemethodsfrommap(Subscriber); go inside:Msubcribermap is a collection of stored maps;copyonwritearraylist < Subscription > is the value of the Msubcribermap map collection, which is a list collection that stores the Subscriber object;
Subscription forsubscriber objects, including subscribers and Target methods; iterates through the list collection of the Subscriber object, removing the specified subscription reference from the collection;ifcopyonwritearraylist<Subscriptionthe > 's list set is null or 0, which removes the deletion from the map by deleting the subscriber, then deleting the contents of the map collection, and the list and map are interrelated.
1  /**2 * Remove subscriber methods from map3      * 4      * @paramSubscriber5      */6      Public voidRemovemethodsfrommap (Object subscriber) {7        //gets the list iterator for the map value8Iterator<copyonwritearraylist<subscription>> Iterator =Msubcribermap9 . VALUES (). iterator ();Ten        //iterating through the collection One          while(Iterator.hasnext ()) { Acopyonwritearraylist<subscription> subscriptions =Iterator.next (); -             //if the collection is not null -               if(Subscriptions! =NULL) { theList<subscription> foundsubscriptions =New -Linkedlist<subscription>(); -                 //Subscription subscriber objects, including subscribers and Target methods -Iterator<subscription> Subiterator =subscriptions.iterator (); +                  while(Subiterator.hasnext ()) { -                     //Get Subscription Object +Subscription Subscription =Subiterator.next (); A                     //Get References atObject CacheObject =subscription.subscriber.get (); -                     if(isobjectsequal (CacheObject, subscriber) -|| CacheObject = =NULL) { -LOG.D ("", "# # # Remove Subscriptions" +Subscriber.getclass (). GetName ()); -                         //foundsubscriptions Adding subscription object references - Foundsubscriptions.add (subscription); in                     } -                 } to                 //Remove the associated subscription[of the Subscriber, which is the value of the list specified in the Map collection, in which a traversal is deleted] + Subscriptions.removeall (foundsubscriptions); -             } the             //if the number of subscribers to an event is empty, you need to clear from the map *             if(Subscriptions = =NULL|| Subscriptions.size () = = 0) { $ Iterator.remove ();Panax Notoginseng             } -         } the}

"The fifth article" Androideventbus Source code reading and analysis of unregister code analysis

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.