前言
在上一文我介紹了EventBus的基礎(chǔ)知識(shí)以及如何使用EventBus3.0+ 使用入門(mén)挑势,但EventBus是如何工作的潮饱?還尚且不知香拉,今天就來(lái)一探究竟凫碌!凡事都有先后順序盛险,分析代碼也一樣,我們?cè)谑褂肊ventBus的時(shí)候一般是按照以下順序執(zhí)行:
?1楔壤、定義事件
?2蹲嚣、訂閱事件
?3端铛、解除訂閱
那我也按照這個(gè)順序進(jìn)行解析禾蚕。
定義事件
其實(shí)這個(gè)沒(méi)什么好分析换淆,簡(jiǎn)單的講就是定義一個(gè)Bean對(duì)象倍试,用來(lái)發(fā)送所需的數(shù)據(jù)县习。
訂閱事件
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main)躁愿;
// 注冊(cè)事件
EventBus.getDefault().register(this)彤钟;
}
在項(xiàng)目使用中逸雹,我們都是在Activty或者Fragmengt的onCreate中通過(guò)EventBus.getDefault().register(this)注冊(cè)事件梆砸,那便從getDefault()開(kāi)始帖世,一探究竟狮暑。
1搬男、EventBus#getDefault()
static volatile EventBus defaultInstance;
public static EventBus getDefault() {
EventBus instance = defaultInstance;
if (instance == null) {
synchronized (EventBus.class) {
instance = EventBus.defaultInstance;
if (instance == null) {
instance = EventBus.defaultInstance = new EventBus();
}
}
}
return instance;
}
這是我們?cè)偈煜げ贿^(guò)的采用雙重校驗(yàn)并加鎖的單例模式备埃,volatile關(guān)鍵字是為了防止處理器亂序執(zhí)行而導(dǎo)致DCL(雙重校驗(yàn)并加鎖)失效的問(wèn)題按脚。
public EventBus() {
this(DEFAULT_BUILDER);
}
EventBus(EventBusBuilder builder) {
logger = builder.getLogger();
subscriptionsByEventType = new HashMap<>();
typesBySubscriber = new HashMap<>();
stickyEvents = new ConcurrentHashMap<>();
mainThreadSupport = builder.getMainThreadSupport();
mainThreadPoster = mainThreadSupport != null ? mainThreadSupport.createPoster(this) : null;
backgroundPoster = new BackgroundPoster(this);
asyncPoster = new AsyncPoster(this);
indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0;
subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
builder.strictMethodVerification, builder.ignoreGeneratedIndex);
logSubscriberExceptions = builder.logSubscriberExceptions;
logNoSubscriberMessages = builder.logNoSubscriberMessages;
sendSubscriberExceptionEvent = builder.sendSubscriberExceptionEvent;
sendNoSubscriberEvent = builder.sendNoSubscriberEvent;
throwSubscriberException = builder.throwSubscriberException;
eventInheritance = builder.eventInheritance;
executorService = builder.executorService;
}
在構(gòu)造方法中主要做了一些變量的初始化。我們還是主要看一下在register中EventBus做了哪些操作脖旱。
2堪遂、EventBus#register()
public void register(Object subscriber) {
// 通過(guò)我們傳入的訂閱者對(duì)象,獲取訂閱者class類(lèi)的實(shí)例萌庆。
Class<?> subscriberClass = subscriber.getClass();
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
由上述代碼可以看出溶褪,EventBus通過(guò)傳入的訂閱者實(shí)例獲取訂閱者class類(lèi)的對(duì)象。并通過(guò)SubscriberMethodFinder類(lèi)查找出這個(gè)class對(duì)象中所有訂閱的方法践险。
3猿妈、SubscriberMethodFinder#findSubscriberMethods()
private static final Map<Class<?>, List<SubscriberMethod>> METHOD_CACHE = new
ConcurrentHashMap<>();
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) {
return subscriberMethods;
}
if (ignoreGeneratedIndex) {
subscriberMethods = findUsingReflection(subscriberClass);
} else {
subscriberMethods = findUsingInfo(subscriberClass);
}
if (subscriberMethods.isEmpty()) {
throw new EventBusException("Subscriber " + subscriberClass
+ " and its super classes have no public methods with the @Subscribe " +
"annotation");
} else {
METHOD_CACHE.put(subscriberClass, subscriberMethods);
return subscriberMethods;
}
}
METHOD_CACHE是用來(lái)緩存訂閱方法的巍虫,key就是傳入的訂閱者class類(lèi)的對(duì)象彭则。如果緩存中有則直接獲取并返回。
如果是第一次創(chuàng)建垫言,因?yàn)镋ventBus中指定ignoreGeneratedIndex默認(rèn)為false贰剥,則會(huì)進(jìn)入到findUsingInfo()這個(gè)方法中。
4筷频、SubscriberMethodFinder#findUsingInfo()
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
// 4.1
FindState findState = prepareFindState();
// 4.2
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
// 4.3
findState.subscriberInfo = getSubscriberInfo(findState);
if (findState.subscriberInfo != null) {
SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
for (SubscriberMethod subscriberMethod : array) {
if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
findState.subscriberMethods.add(subscriberMethod);
}
}
} else {
findUsingReflectionInSingleClass(findState);
}
// 4.4 循環(huán)查找父類(lèi),如果遇到系統(tǒng)類(lèi)時(shí)會(huì)跳過(guò)前痘,提高查找速度凛捏,提升性能
findState.moveToSuperclass();
}
return getMethodsAndRelease(findState);
}
其中FindState是一個(gè)內(nèi)部類(lèi),用來(lái)保存訂閱者類(lèi)相關(guān)的信息芹缔。
4.1坯癣、SubscriberMethodFinder#prepareFindState()
private static final FindState[] FIND_STATE_POOL = new FindState[POOL_SIZE];
private FindState prepareFindState() {
synchronized (FIND_STATE_POOL) {
for (int i = 0; i < POOL_SIZE; i++) {
FindState state = FIND_STATE_POOL[i];
if (state != null) {
FIND_STATE_POOL[i] = null;
return state;
}
}
}
return new FindState();
}
prepareFindState()用來(lái)創(chuàng)建FindState對(duì)象,首先從FindState池中獲取最欠,如果有則直接返回示罗,并且將該下標(biāo)的對(duì)象置空(當(dāng)獲取到訂閱方法并且開(kāi)始發(fā)布時(shí)又會(huì)重新賦值)惩猫,沒(méi)有就重新new一個(gè)FindState對(duì)象。
4.2蚜点、SubscriberMethodFinder#FindState#initForSubscriber()
static class FindState {
final List<SubscriberMethod> subscriberMethods = new ArrayList<>();
final Map<Class, Object> anyMethodByEventType = new HashMap<>();
final Map<String, Class> subscriberClassByMethodKey = new HashMap<>();
final StringBuilder methodKeyBuilder = new StringBuilder(128);
Class<?> subscriberClass;
Class<?> clazz;
boolean skipSuperClasses;
SubscriberInfo subscriberInfo;
void initForSubscriber(Class<?> subscriberClass) {
this.subscriberClass = clazz = subscriberClass;
skipSuperClasses = false;
subscriberInfo = null;
}
...
}
初始化FindState對(duì)象轧房。
?1. subscriberMethods保存SubscriberMethod對(duì)象的列表绍绘。
4.3奶镶、SubscriberMethodFinder#getSubscriberInfo
private SubscriberInfo getSubscriberInfo(FindState findState) {
if (findState.subscriberInfo != null && findState.subscriberInfo.getSuperSubscriberInfo()
!= null) {
SubscriberInfo superclassInfo = findState.subscriberInfo.getSuperSubscriberInfo();
if (findState.clazz == superclassInfo.getSubscriberClass()) {
return superclassInfo;
}
}
if (subscriberInfoIndexes != null) {
for (SubscriberInfoIndex index : subscriberInfoIndexes) {
SubscriberInfo info = index.getSubscriberInfo(findState.clazz);
if (info != null) {
return info;
}
}
}
return null;
}
這個(gè)方法主要是找到訂閱者信息是否存在,如果存在就直接返回陪拘,第一次創(chuàng)建返回null厂镇。所以上面的findUsingInfo會(huì)走到else中,并調(diào)用findUsingReflectionInSingleClass()方法左刽。
4.4 SubscriberMethodFinder#FindState#moveToSuperclass()
void moveToSuperclass() {
if (skipSuperClasses) {
clazz = null;
} else {
clazz = clazz.getSuperclass();
String clazzName = clazz.getName();
/** Skip system classes, this just degrades performance. */
if (clazzName.startsWith("java.") || clazzName.startsWith("javax.") || clazzName
.startsWith("android.")) {
clazz = null;
}
}
}
5捺信、SubscriberMethodFinder#findUsingReflectionInSingleClass
private void findUsingReflectionInSingleClass(FindState findState) {
Method[] methods;
try {
// This is faster than getMethods, especially when subscribers are fat classes like
// Activities
// 找到訂閱者類(lèi)中所有的方法
methods = findState.clazz.getDeclaredMethods();
} catch (Throwable th) {
// Workaround for java.lang.NoClassDefFoundError, see https://github
// .com/greenrobot/EventBus/issues/149
methods = findState.clazz.getMethods();
findState.skipSuperClasses = true;
}
for (Method method : methods) {
// 獲取方法的修飾符int值
int modifiers = method.getModifiers();
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
// 返回方法的參數(shù)類(lèi)型數(shù)組
Class<?>[] parameterTypes = method.getParameterTypes();
// 如果參數(shù)只有1個(gè)
if (parameterTypes.length == 1) {
// 獲取該方法的注解
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
// 如果包含@Subscribe注解
if (subscribeAnnotation != null) {
// 獲取訂閱方法的事件類(lèi)型
Class<?> eventType = parameterTypes[0];
if (findState.checkAdd(method, eventType)) {
// 獲取訂閱方法的線(xiàn)程模型
ThreadMode threadMode = subscribeAnnotation.threadMode();
// 5.1 保存訂閱者信息
findState.subscriberMethods.add(new SubscriberMethod(method,
eventType, threadMode,
subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
}
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe
.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method
.getName();
throw new EventBusException("@Subscribe method " + methodName +
"must have exactly 1 parameter but has " + parameterTypes.length);
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException(methodName +
" is a illegal @Subscribe method: must be public, non-static, and " +
"non-abstract");
}
}
}
這個(gè)方法的主要是作用是通過(guò)反射找出訂閱者類(lèi)中所有的訂閱方法,并保存訂閱者的相關(guān)信息到FindState對(duì)象的subscriberMethods列表中欠痴,包括訂閱方法残黑、事件類(lèi)型、線(xiàn)程模型斋否、優(yōu)先級(jí)梨水、粘黏性。
5.1 SubscriberMethod#SubscriberMethod()
public class SubscriberMethod {
final Method method;
final ThreadMode threadMode;
final Class<?> eventType;
final int priority;
final boolean sticky;
/** Used for efficient comparison */
String methodString;
public SubscriberMethod(Method method, Class<?> eventType, ThreadMode threadMode, int priority, boolean sticky) {
this.method = method;
this.threadMode = threadMode;
this.eventType = eventType;
this.priority = priority;
this.sticky = sticky;
}
...
}
至此茵臭,當(dāng)findUsingInfo的循環(huán)結(jié)束時(shí)疫诽,通過(guò)Register()傳進(jìn)來(lái)的訂閱者類(lèi)的對(duì)象,查找到訂閱者類(lèi)中所有的訂閱方法以及相關(guān)信息就結(jié)束了旦委。
6奇徒、SubscriberMethodFinder#getMethodsAndRelease()
private List<SubscriberMethod> getMethodsAndRelease(FindState findState) {
List<SubscriberMethod> subscriberMethods = new ArrayList<>(findState.subscriberMethods);
findState.recycle();
synchronized (FIND_STATE_POOL) {
for (int i = 0; i < POOL_SIZE; i++) {
if (FIND_STATE_POOL[i] == null) {
FIND_STATE_POOL[i] = findState;
break;
}
}
}
return subscriberMethods;
}
這個(gè)方法的主要作用是對(duì)findState做初始化處理,釋放內(nèi)存缨硝,將findState保存到FindState池之前被置空的下標(biāo)中摩钙,方便下一次使用。然后返回SubscriberMethod對(duì)象列表查辩,保存到METHOD_CACHE中胖笛。
7、EventBus#subscribe()
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
// 循環(huán)訂閱方法列表
for (SubscriberMethod subscriberMethod : subscriberMethods) {
// 訂閱
subscribe(subscriber, subscriberMethod);
}
}
}
// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
// 首先獲取訂閱方法的事件類(lèi)型
Class<?> eventType = subscriberMethod.eventType;
// 新建一個(gè)Subscription對(duì)象
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
// 1. 如果 subscriptionsByEventType 中沒(méi)有以該事件類(lèi)型作為key值的存在宜岛,則添加长踊。
if (subscriptions == null) {
subscriptions = new CopyOnWriteArrayList<>();
subscriptionsByEventType.put(eventType, subscriptions);
} else {
// 避免訂閱同一事件
if (subscriptions.contains(newSubscription)) {
throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
+ eventType);
}
}
int size = subscriptions.size();
for (int i = 0; i <= size; i++) {
// 按照優(yōu)先級(jí)添加,等級(jí)越高在List中位置越靠前
if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
subscriptions.add(i, newSubscription);
break;
}
}
// 2. 將訂閱者類(lèi)中所有的事件類(lèi)型保存在map中萍倡,訂閱者類(lèi)的對(duì)象作為key值身弊,事件類(lèi)型列表作為value。
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
subscribedEvents.add(eventType);
// 3. 判斷是否是粘性事件
if (subscriberMethod.sticky) {
// 默認(rèn)為true
if (eventInheritance) {
// Existing sticky events of all subclasses of eventType have to be considered.
// Note: Iterating over all events may be inefficient with lots of sticky events,
// thus data structure should be changed to allow a more efficient lookup
// (e.g. an additional map storing sub classes of super classes: Class -> List<Class>).
Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
for (Map.Entry<Class<?>, Object> entry : entries) {
Class<?> candidateEventType = entry.getKey();
if (eventType.isAssignableFrom(candidateEventType)) {
Object stickyEvent = entry.getValue();
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
} else {
Object stickyEvent = stickyEvents.get(eventType);
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
}
我們把焦點(diǎn)再次聚焦到register()方法中,當(dāng)EventBus找到訂閱者類(lèi)中所有的訂閱方法時(shí)阱佛,會(huì)調(diào)用subscribe()方法帖汞。在subscribe()方法中主要有三個(gè)部分,分別對(duì)應(yīng)三個(gè)if判斷凑术,在上面代碼中都有一一講解翩蘸。
EventBus的事件訂閱到此就分析結(jié)束了,接下來(lái)要講的是事件的發(fā)布麦萤,它是怎么發(fā)布事件的鹿鳖?又是怎么切換線(xiàn)程的?
8壮莹、EventBus#post()
public void post(Object event) {
// 獲取PostingThreadState對(duì)象
PostingThreadState postingState = currentPostingThreadState.get();
List<Object> eventQueue = postingState.eventQueue;
eventQueue.add(event);
// 判斷當(dāng)前事件是否正在發(fā)布狀態(tài)翅帜,避免連續(xù)重復(fù)操作。
if (!postingState.isPosting) {
// 是否是主線(xiàn)程
postingState.isMainThread = isMainThread();
postingState.isPosting = true;
if (postingState.canceled) {
throw new EventBusException("Internal error. Abort state was not reset");
}
try {
while (!eventQueue.isEmpty()) {
postSingleEvent(eventQueue.remove(0), postingState);
}
} finally {
// 發(fā)布完成后回到初始狀態(tài)
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
首先獲取一個(gè)PostingThreadState對(duì)象命满,里面包含了事件隊(duì)列eventQueue涝滴,然后把當(dāng)前事件添加到事件隊(duì)列中,如果當(dāng)前事件沒(méi)有處在發(fā)布狀態(tài)胶台,在則會(huì)進(jìn)入到if判斷中歼疮,最終會(huì)調(diào)用postSingleEvent()方法,并且通過(guò)eventQueue.remove(0)刪除已經(jīng)發(fā)布的事件诈唬。
9韩脏、EventBus#postSingleEvent()
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
// 獲取事件類(lèi)的Class類(lèi)型
Class<?> eventClass = event.getClass();
boolean subscriptionFound = false;
// 默認(rèn)為true
if (eventInheritance) {
List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
int countTypes = eventTypes.size();
// 遍歷所有事件,包括這個(gè)事件類(lèi)本身铸磅、父類(lèi)以及實(shí)現(xiàn)的接口
for (int h = 0; h < countTypes; h++) {
Class<?> clazz = eventTypes.get(h);
subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);
}
} else {
subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);
}
// 不存在訂閱該事件的訂閱者
if (!subscriptionFound) {
if (logNoSubscriberMessages) {
logger.log(Level.FINE, "No subscribers registered for event " + eventClass);
}
if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
eventClass != SubscriberExceptionEvent.class) {
post(new NoSubscriberEvent(this, event));
}
}
}
9.1赡矢、EventBus#lookupAllEventTypes()
private static List<Class<?>> lookupAllEventTypes(Class<?> eventClass) {
synchronized (eventTypesCache) {
// 獲取和這個(gè)事件類(lèi)相關(guān)的class列表(包括父類(lèi)和接口)
List<Class<?>> eventTypes = eventTypesCache.get(eventClass);
if (eventTypes == null) {
eventTypes = new ArrayList<>();
Class<?> clazz = eventClass;
// 循環(huán)添加這個(gè)事件類(lèi)的父類(lèi)和接口,并作為事件保存和發(fā)送
while (clazz != null) {
eventTypes.add(clazz);
addInterfaces(eventTypes, clazz.getInterfaces());
clazz = clazz.getSuperclass();
}
eventTypesCache.put(eventClass, eventTypes);
}
return eventTypes;
}
}
這個(gè)方法主要是獲取Event事件類(lèi)及其父類(lèi)和接口的所有class列表阅仔,并以key/value的形式添加到eventTypesCache這個(gè)緩存對(duì)象性中吹散,方便下次直接使用,提升性能八酒。
10空民、EventBus#postSingleEventForEventType()
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
// 根據(jù)事件類(lèi)型獲取對(duì)應(yīng)Subscription列表
subscriptions = subscriptionsByEventType.get(eventClass);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
// 遍歷列表
for (Subscription subscription : subscriptions) {
postingState.event = event;
postingState.subscription = subscription;
boolean aborted = false;
try {
postToSubscription(subscription, event, postingState.isMainThread);
aborted = postingState.canceled;
} finally {
// 回到初始狀態(tài)
postingState.event = null;
postingState.subscription = null;
postingState.canceled = false;
}
if (aborted) {
break;
}
}
return true;
}
return false;
}
postSingleEventForEventType()方法主要是獲取訂閱了該事件類(lèi)型的所有的Subscription列表,然后遍歷列表對(duì)里面的每個(gè)對(duì)象調(diào)用postToSubscription()方法羞迷。
11界轩、EventBus#postToSubscription()
private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
switch (subscription.subscriberMethod.threadMode) {
case POSTING:
invokeSubscriber(subscription, event);
break;
case MAIN:
if (isMainThread) {
invokeSubscriber(subscription, event);
} else {
mainThreadPoster.enqueue(subscription, event);
}
break;
case MAIN_ORDERED:
if (mainThreadPoster != null) {
mainThreadPoster.enqueue(subscription, event);
} else {
// temporary: technically not correct as poster not decoupled from subscriber
invokeSubscriber(subscription, event);
}
break;
case BACKGROUND:
if (isMainThread) {
backgroundPoster.enqueue(subscription, event);
} else {
invokeSubscriber(subscription, event);
}
break;
case ASYNC:
asyncPoster.enqueue(subscription, event);
break;
default:
throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
}
}
到了這里EventBus的事件發(fā)布解析就結(jié)束了。在這個(gè)方法中首先獲取當(dāng)前事件的訂閱方法的線(xiàn)程模型闭树,然后根據(jù)線(xiàn)程模型選擇對(duì)應(yīng)的線(xiàn)程去完成事件的發(fā)布耸棒。invokeSubscriber()主要是利用反射去調(diào)用方法,這樣就實(shí)現(xiàn)了訂閱事件->發(fā)布事件->接收事件這一過(guò)程报辱。
12、EventBus#unregister()
public synchronized void unregister(Object subscriber) {
// 獲取當(dāng)前訂閱者訂閱的所有事件
List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
if (subscribedTypes != null) {
// 遍歷所有的訂閱事件
for (Class<?> eventType : subscribedTypes) {
unsubscribeByEventType(subscriber, eventType);
}
typesBySubscriber.remove(subscriber);
} else {
logger.log(Level.WARNING, "Subscriber to unregister was not registered before: " + subscriber.getClass());
}
}
這里主要是把訂閱者傳進(jìn)去,得到當(dāng)前訂閱者訂閱的所有的事件列表碍现,遍歷是列表移除與訂閱者相關(guān)的所有信息幅疼。
12.1、EventBus#unsubscribeByEventType()
private void unsubscribeByEventType(Object subscriber, Class<?> eventType) {
// 獲取事件類(lèi)型對(duì)應(yīng)的Subscription集合
List<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions != null) {
int size = subscriptions.size();
// 遍歷subscriptions列表昼接,移除所有信息
for (int i = 0; i < size; i++) {
Subscription subscription = subscriptions.get(i);
if (subscription.subscriber == subscriber) {
subscription.active = false;
subscriptions.remove(i);
i--;
size--;
}
}
}
}
總結(jié)
到此爽篷,EventBus的分析就全部結(jié)束了,核心部分集中在訂閱和發(fā)布慢睡。訂閱主要是為了找到所有的訂閱方法和事件并保存相互關(guān)聯(lián)的信息逐工,發(fā)布主要是從保存的相互關(guān)聯(lián)的信息當(dāng)中找到以被發(fā)布的事件類(lèi)型作為參數(shù)的所有訂閱方法并通過(guò)反射調(diào)用該方法。