The code is written off Eventbus generally we will write in Ondestory:
- 1 eventbus.getdefault (). Unregister (this);
then go to unregister inside to see:
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