Android EventBus3.0使用及原始碼解析
本文已授權微信公眾號《非著名程式設計師》原創首發,轉載請務必註明出處。
叨了個叨
最近因為換工作的一些瑣事搞的我一個頭兩個大,也沒怎麼去學新東西,實在是有些愧疚。新專案用到了EventBus3.0,原來只是聽說EventBus的鼎鼎大名,一直沒仔細研究過。趁著週末有些時間,研究下程式碼,也算沒有虛度光陰。
EventBus3.0簡介
EventBus
是greenrobot出品的一個用於Android中事件釋出/訂閱的庫。以前傳遞物件可能通過介面、廣播、檔案等等,尤其像同一個Activity
兩個Fragment
之間採用介面傳遞物件,十分的麻煩,而且耦合度較高。使用EventBus
可以看到,釋出者(
Publisher
)使用post()
方法將Event
傳送到Event Bus
,而後Event Bus
自動將Event
傳送到多個訂閱者(Subcriber
)。這裡需要注意兩個地方:(1)一個釋出者可以對應多個訂閱者。(2)3.0以前訂閱者的訂閱方法為onEvent()
、onEventMainThread()
、onEventBackgroundThread()
和onEventAsync()
。在Event Bus3.0
之後統一採用註解@Subscribe
的形式,具體實現方式見下文。
EventBus3.0的使用
新建兩個Activity
,花3s掃一下即可。程式碼如下:
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 註冊EventBus EventBus.getDefault().register(this); startActivity(new Intent(this,SecondActivity.class)); } @Override protected void onDestroy() { super.onDestroy(); // 反註冊EventBus EventBus.getDefault().unregister(this); } // 主執行緒呼叫 @Subscribe(threadMode = ThreadMode.MAIN) public void eventBusMain(String str){ Log.i("TAG", "MAIN:"+str+" Thread="+Thread.currentThread().getId()); } // 1.釋出執行緒為主執行緒,新開執行緒呼叫 // 2.釋出執行緒為子執行緒,釋出執行緒呼叫 @Subscribe(threadMode = ThreadMode.BACKGROUND) public void eventBusBg(String str){ Log.i("TAG", "BACKGROUND:"+str+" Thread="+Thread.currentThread().getId()); } // 在釋出執行緒呼叫,預設值 @Subscribe(threadMode = ThreadMode.POSTING) public void eventBusPosting(String str){ Log.i("TAG", "POSTING:"+str+" Thread="+Thread.currentThread().getId()); } // 每次都新開執行緒呼叫 @Subscribe(threadMode = ThreadMode.ASYNC) public void eventBusAsync(String str){ Log.i("TAG", "ASYNC:"+str+" Thread="+Thread.currentThread().getId()); } } public class SecondActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); EventBus.getDefault().post("from second activity mainThread: info"); Log.i("TAG", "Post thread="+Thread.currentThread().getId()); new Thread(new Runnable() { @Override public void run() { EventBus.getDefault().post("from second activity childThread: info"); Log.i("TAG", "Post thread="+Thread.currentThread().getId()); } }).start(); } }
在MainActivity
的onCreate()
/onDestroy()
中分別註冊/反註冊EventBus
。然後寫了四個測試ThreadMode
的方法,呼叫時機註釋的很清楚,就不贅述了。最後在SecondActivity
的主執行緒和子執行緒中分別呼叫Post()
方法,注意,這裡Post()
方法的引數為Object
型別,這也就意味著我們傳遞任何物件都是可以的,例如JavaBean
、List<E>
等等都是可以的,這裡為了方便演示直接傳遞了String
。Log資訊如下:
第一張圖中釋出者傳送執行緒為主執行緒,即Post thread = 1
,在訂閱者收到訊息時,ThreadMode = Main
和ThreadMode = Posting
的方法都在主執行緒呼叫,ASYNC
和BACKGROUND
都新開了執行緒。圖二對比註釋同理。
除此之外,Subscribe
註解還支援priority
和sticky
屬性。priority
設定接收者的優先順序,預設值為0。優先順序高的方法先被呼叫,在方法呼叫完成後可以呼叫EventBus.getDefault().cancelEventDelivery(event) ;
終止優先順序低的方法的呼叫。sticky
為粘性事件,預設為關閉狀態。能夠收到訂閱之前傳送到的最後一條訊息,並且傳送的方法不再是post()
而是postSticky()
。
EventBus3.0原始碼解析
EventBus
是Very的好用。耦合度大大的降低,而且程式碼十分優雅。它是怎麼就做到了這麼優雅的呢?知其然,知其所以然。下面就開始一步步的分析。
註解標籤Subscribe
對註解不瞭解的同學可以看下這篇部落格。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Subscribe {
ThreadMode threadMode() default ThreadMode.POSTING;
/**
* If true, delivers the most recent sticky event (posted with
* {@link EventBus#postSticky(Object)}) to this subscriber (if event available).
*/
boolean sticky() default false;
/** Subscriber priority to influence the order of event delivery.
* Within the same delivery thread ({@link ThreadMode}), higher priority subscribers will receive events before
* others with a lower priority. The default priority is 0. Note: the priority does *NOT* affect the order of
* delivery among subscribers with different {@link ThreadMode}s! */
int priority() default 0;
}
public enum ThreadMode {
POSTING,
MAIN,
BACKGROUND,
ASYNC
}
註解Subscribe
在執行時解析,且只能加在METHOD
上。其中有三個方法,threadMode()
返回型別ThreadMode
為列舉型別,預設值為POSTING
,sticky()
預設返回false
,priority()
預設返回0。
1. Register
流程
EventBus#getDefault()
public EventBus() {
this(DEFAULT_BUILDER);
}
public static EventBus getDefault() {
if (defaultInstance == null) {
synchronized (EventBus.class) {
if (defaultInstance == null) {
defaultInstance = new EventBus();
}
}
}
return defaultInstance;
}
EventBus
採用雙重校驗鎖設計為一個單例模式,奇怪的在於雖然設計為單例模式,但是構造方法確實public
型別,這不是坑爹嘛!難道greenrobot
在設計EventBus
獲取例項方法的時候在打LOL,一不小心打錯了?原來啊,EventBus
預設支援一條事件匯流排,通常是通過getDefault()
方法獲取EventBus
例項,但也能通過直接new EventBus
這種最簡單的方式獲取多條事件匯流排,彼此之間完全分開。設計之思想不禁讓人拍案叫絕。
EventBus#register()
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
首先得到訂閱者的報名.類名,即哪個具體類註冊。然後呼叫subscriberMethodFinder.findSubscriberMethods(subscriberClass)
,從方法名和返回值來看,findSubscriberMethods()
的作用應該是遍歷查詢訂閱者中所有的訂閱方法。
SubscriberMethodFinder#findSubscriberMethods()
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
// 查詢快取
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) {
// 快取中有則直接返回
return subscriberMethods;
}
// 預設false
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;
}
}
注意subscriberMethods.isEmpty()
,如果註冊了EventBus
,但卻沒有使用註解Subscribe
是會出現EventBusException
異常的。下面跟進findUsingInfo()
方法。
SubscriberMethodFinder#findUsingInfo()
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
// 從FIND_STATE_POOL陣列中查詢FindState,命中返回,否則直接new
FindState findState = prepareFindState();
// 初始化FindState
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
findState.subscriberInfo = getSubscriberInfo(findState);
// findState.subscriberInfo預設null
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);
}
// 將findState.clazz變為改類的父類
findState.moveToSuperclass();
}
return getMethodsAndRelease(findState);
}
// SubscriberMethodFinder$FindState#initForSubscriber()
void initForSubscriber(Class<?> subscriberClass) {
this.subscriberClass = clazz = subscriberClass;
skipSuperClasses = false;
subscriberInfo = null;
}
findState.subscriberInfo
預設null
,那麼就進入到findUsingReflectionInSingleClass(findState)
,先看下這個方法,等下還要返回來看。
SubscriberMethodFinder#findUsingReflectionInSingleClass()
private static final int MODIFIERS_IGNORE = Modifier.ABSTRACT | Modifier.STATIC | BRIDGE | SYNTHETIC;
private void findUsingReflectionInSingleClass(FindState findState) {
Method[] methods;
try {
// This is faster than getMethods, especially when subscribers are fat classes like Activities
// 獲取到類中所有的方法
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 modifiers = method.getModifiers();
// 必須被public修飾,而且不能為MODIFIERS_IGNORE
// MODIFIERS_IGNORE定義為ABSTRACT、STATIC、BRIDGE、SYNTHETIC。
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
// 獲取方法所有引數型別
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length == 1) {
// 是否有Subscribe註解標籤
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
if (subscribeAnnotation != null) {
// 帶有Subscribe註解標籤的方法的第一個引數型別
Class<?> eventType = parameterTypes[0];
// 關聯method, eventType到anyMethodByEventType
if (findState.checkAdd(method, eventType)) {
ThreadMode threadMode = subscribeAnnotation.threadMode();
// 構造SubscriberMethod,並且新增到findState.subscriberMethods
findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
}
}
// strictMethodVerification 預設為false,不會丟擲異常,但還是建議符合規範
} 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");
}
}
}
// `SubscriberMethodFinder#checkAdd()`
boolean checkAdd(Method method, Class<?> eventType) {
// 2 level check: 1st level with event type only (fast), 2nd level with complete signature when required.
// Usually a subscriber doesn't have methods listening to the same event type.
Object existing = anyMethodByEventType.put(eventType, method);
if (existing == null) {
return true;
} else {
...
}
接下來返回SubscriberMethodFinder#findUsingInfo()
接著看,在findUsingInfo()
中迴圈執行完後return getMethodsAndRelease(findState)
static class FindState {
final List<SubscriberMethod> subscriberMethods = new ArrayList<>();
...
}
private List<SubscriberMethod> getMethodsAndRelease(FindState findState) {
List<SubscriberMethod> subscriberMethods = new ArrayList<>(findState.subscriberMethods);
// 置空findState
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;
}
在getMethodsAndRelease()
中將findState
置空,存放進FIND_STATE_POOL
陣列,最後返回findState.subscriberMethods
。返回EventBus#register()
。
EventBus#register()
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
呼叫SubscriberMethodFinder#findSubscriberMethods()
後,以List<SubscriberMethod>
形式返回了訂閱者所有的訂閱事件。然後遍歷執行subscribe()
方法。看樣子應該是遍歷List<SubscriberMethod>
,然後將訂閱者和訂閱事件繫結。沒撒好說的,跟進subscribe()
EventBus#subscribe()
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
// 獲取訂閱事件的型別,即訂閱方法中的唯一引數型別
Class<?> eventType = subscriberMethod.eventType;
// 用訂閱者和訂閱方法構造一個Subscription物件
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
// 查詢所有的訂閱了訂閱事件的訂閱者
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
// 沒有訂閱者訂閱過則新建個CopyOnWriteArrayList<subscriptions>,並put進subscriptionsByEventType PS:CopyOnWriteArrayList支援併發讀寫
if (subscriptions == null) {
subscriptions = new CopyOnWriteArrayList<>();
subscriptionsByEventType.put(eventType, subscriptions);
} else {
// 訂閱者List不為空,而且已經包含了newSubscription,則會丟擲異常。即:訂閱者不能重複訂閱同一事件
if (subscriptions.contains(newSubscription)) {
throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
+ eventType);
}
}
int size = subscriptions.size();
// 根據訂閱者優先順序,增加到訂閱者列表subscriptions的相應位置
for (int i = 0; i <= size; i++) {
if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
subscriptions.add(i, newSubscription);
break;
}
}
// 獲取訂閱者所有訂閱事件的列表,預設為null
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
// 將訂閱事件新增進對應訂閱者的訂閱列表
subscribedEvents.add(eventType);
// sticky預設為false
if (subscriberMethod.sticky) {
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);
}
}
}
EventBus#Register()
其實只做了三件事:
1. 查詢訂閱者所有的訂閱事件
2. 將訂閱事件作為key
,所有訂閱了此訂閱事件的訂閱者作為value
存放進subscriptionsByEventType
3. 將訂閱者作為key
,訂閱者的所有訂閱事件作為value
存放進typesBySubscriber
至此,EventBus.getDefault().register(this)
流程完畢。
2. Post
流程
EventBus#getDefault()
獲取EventBus例項
。和Register
流程中一樣,不再贅述。
EventBus#post()
/** Posts the given event to the event bus. */
public void post(Object event) {
// 依據不同的執行緒獲取相應的剛初始化的PostingThreadState
PostingThreadState postingState = currentPostingThreadState.get();
List<Object> eventQueue = postingState.eventQueue;
// 將event加入到postingState.eventQueue
eventQueue.add(event);
// isPosting預設false
if (!postingState.isPosting) {
// 判斷是否是主執行緒
postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();
postingState.isPosting = true;
if (postingState.canceled) {
throw new EventBusException("Internal error. Abort state was not reset");
}
try {
// 遍歷傳送eventQueue中的event
while (!eventQueue.isEmpty()) {
postSingleEvent(eventQueue.remove(0), postingState);
}
} finally {
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
上面程式碼中currentPostingThreadState
為ThreadLocal<PostingThreadState>
物件,對ThreadLocal<>
機制不瞭解的同學,可以檢視這篇部落格。下面跟進postSingleEvent()
方法。
EventBus#postSingleEvent()
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
// 獲取event的型別
Class<?> eventClass = event.getClass();
boolean subscriptionFound = false;
// eventInheritance預設為true
if (eventInheritance) {
// 依據訂閱事件型別,將訂閱事件型別及所有父類新增進eventTypes。詳情見下文EventBus.lookupAllEventTypes()分析
List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
int countTypes = eventTypes.size();
// 遍歷countTypes,通過呼叫postSingleEventForEventType()方法通知所有訂閱者
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) {
Log.d(TAG, "No subscribers registered for event " + eventClass);
}
if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
eventClass != SubscriberExceptionEvent.class) {
post(new NoSubscriberEvent(this, event));
}
}
}
EventBus#lookupAllEventTypes()
private static List<Class<?>> lookupAllEventTypes(Class<?> eventClass) {
synchronized (eventTypesCache) {
// 根據訂閱事件查詢所有自身及父類,eventTypes預設為null
List<Class<?>> eventTypes = eventTypesCache.get(eventClass);
if (eventTypes == null) {
eventTypes = new ArrayList<>();
Class<?> clazz = eventClass;
while (clazz != null) {
// 將訂閱事件新增進eventTypes
eventTypes.add(clazz);
// 遍歷訂閱事件的所有父類,依次新增進eventTypes
addInterfaces(eventTypes, clazz.getInterfaces());
clazz = clazz.getSuperclass();
}
// 將訂閱事件和包含訂閱事件自身及所有父類的eventTypes新增進eventTypesCache
eventTypesCache.put(eventClass, eventTypes);
}
return eventTypes;
}
}
現在假設傳遞的資料為Person
類,而Person
類實現了IPerson
介面。通過上面的分析可以得出結論:在傳遞物件(Person
)的時候,訂閱事件中引數為被傳遞物件的所有父類訂閱事件(IPerson
)也都會被呼叫。筆者已經驗證通過,感興趣的同學可以再驗證一下。
EventBus#postSingleEventForEventType()
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
// 根據訂閱事件查詢所有已經註冊過的訂閱者
// 注意:這裡第一次傳遞進來的是訂閱事件,之後會逐個傳遞進來訂閱事件的父類
subscriptions = subscriptionsByEventType.get(eventClass);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
for (Subscription subscription : subscriptions) {
postingState.event = event;
postingState.subscription = subscription;
boolean aborted = false;
try {
// 引數解釋:subscription-被遍歷到的訂閱者;event-訂閱事件引數(子類);
postToSubscription(subscription, event, postingState.isMainThread);
aborted = postingState.canceled;
} finally {
postingState.event = null;
postingState.subscription = null;
postingState.canceled = false;
}
if (aborted) {
break;
}
}
return true;
}
return false;
}
在EventBus#register()
最後總結道:將訂閱事件作為key
,所有訂閱了此訂閱事件的訂閱者作為value
存放進subscriptionsByEventType
。這裡就依據訂閱事件然後查詢對應所有的訂閱者。注意:由於遍歷訂閱事件引數所有父類的原因,一個訂閱事件的Post
第一次執行postToSubscription()
時,subscription
引數,遍歷時為訂閱事件的訂閱者。之後再呼叫postToSubscription()
時,subscription
引數都為訂閱時間父類的訂閱者。而event
引數則一直是訂閱事件中的唯一引數(最底層子類)。
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 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);
}
}
看到這裡差不多可以鬆口氣,終於要分發呼叫訂閱者的訂閱事件了!寫了整整一下午,容我抽支菸再。
首先根據ThreadMode
確定分發型別。這裡以最常用的Main
為例,其餘兩個Poster
同理。如果是isMainThread=true
,那麼直接呼叫invokeSubscriber()
,否則呼叫mainThreadPoster.enqueue()
。下面分別解釋這兩種情況。
EventBus#invokeSubscriber()
void invokeSubscriber(Subscription subscription, Object event) {
try {
// 反射呼叫
subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
} catch (InvocationTargetException e) {
handleSubscriberException(subscription, event, e.getCause());
} catch (IllegalAccessException e) {
throw new IllegalStateException("Unexpected exception", e);
}
}
沒撒好說的,直接反射呼叫訂閱者的訂閱事件。注意:引數event
是子類物件,就算呼叫訂閱事件中唯一引數是引數的父類,那麼傳遞的仍然是子類物件。筆者已經驗證,感興趣的同學可以自行驗證。然後檢視HandlerPoster#enqueue()
。
HandlerPoster#enqueue()
final class HandlerPoster extends Handler {
...
void enqueue(Subscription subscription, Object event) {
// 嘗試從pendingPostPool中獲取pendingPost,沒有則直接new PendingPost
PendingPost pendingPost = PendingPost.obtainPendingPost(subscription, event);
synchronized (this) {
// 加入佇列
queue.enqueue(pendingPost);
if (!handlerActive) {
handlerActive = true;
// 傳送空訊息 呼叫handleMessage
if (!sendMessage(obtainMessage())) {
throw new EventBusException("Could not send handler message");
}
}
}
}
@Override
public void handleMessage(Message msg) {
// 已經切換到主執行緒
while (true) {
// 遍歷獲取queue中的PendingPost物件
PendingPost pendingPost = queue.poll();
...
// 呼叫eventBus.invokeSubscriber
eventBus.invokeSubscriber(pendingPost);
...
}
}
}
EventBus#invokeSubscriber()
void invokeSubscriber(PendingPost pendingPost) {
// 提取訂閱事件
Object event = pendingPost.event;
// 提取訂閱者
Subscription subscription = pendingPost.subscription;
// 釋放pendingPost
PendingPost.releasePendingPost(pendingPost);
if (subscription.active) {
// 反射呼叫訂閱者的訂閱事件
invokeSubscriber(subscription, event);
}
}
至此,訂閱者在相應執行緒呼叫訂閱事件完成,EventBus.getDefault().Post()
流程完畢。
EventBus#Post()
也只做了三件事
1. 根據訂閱事件在subscriptionsByEventType
中查詢相應的訂閱者
2. 分發訂閱者的訂閱事件呼叫執行緒
2. 通過反射呼叫訂閱者的訂閱事件
3. unregister
流程
EventBus#getDefault()
獲取EventBus例項
。和Register
流程中一樣,不再贅述。
EventBus#unregister()
public synchronized void unregister(Object subscriber) {
List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
if (subscribedTypes != null) {
for (Class<?> eventType : subscribedTypes) {
unsubscribeByEventType(subscriber, eventType);
}
typesBySubscriber.remove(subscriber);
} else {
Log.w(TAG, "Subscriber to unregister was not registered before: " + subscriber.getClass());
}
}
EventBus#unsubscribeByEventType()
private void unsubscribeByEventType(Object subscriber, Class<?> eventType) {
List<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions != null) {
int size = subscriptions.size();
for (int i = 0; i < size; i++) {
Subscription subscription = subscriptions.get(i);
if (subscription.subscriber == subscriber) {
subscription.active = false;
subscriptions.remove(i);
i--;
size--;
}
}
}
}
在EventBus#register()
最後總結道:
將訂閱事件作為key
,所有訂閱了此訂閱事件的訂閱者作為value
存放進subscriptionsByEventType
。
將訂閱者作為key
,訂閱者的所有訂閱事件作為value
存放進typesBySubscriber
。
現在要反註冊咯。移除相應的key
、value
即可。EventBus3.0
的使用及原始碼解析到此結束,Have a nice day~