本文已授權微信公衆號《非著名程序員》原創首發,轉載請務必註明出處。
叨了個叨
最近因爲換工作的一些瑣事搞的我一個頭兩個大,也沒怎麼去學新東西,實在是有些愧疚。新項目用到了EventBus3.0,原來只是聽說EventBus的鼎鼎大名,一直沒仔細研究過。趁着週末有些時間,研究下代碼,也算沒有虛度光陰。
EventBus GitHub : https://github.com/greenrobot/EventBus
EventBus3.0簡介
EventBus
是greenrobot出品的一個用於Android中事件發佈/訂閱的庫。以前傳遞對象可能通過接口、廣播、文件等等,尤其像同一個Activity
兩個Fragment
之間採用接口傳遞對象,十分的麻煩,而且耦合度較高。使用EventBus
之後,這些將不再是問題。盜用GiHub上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~