Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android EventBus3.0使用及源碼解析

Android EventBus3.0使用及源碼解析

編輯:關於Android編程

叨了個叨

最近因為換工作的一些瑣事搞的我一個頭兩個大,也沒怎麼去學新東西,實在是有些愧疚。新項目用到了EventBus3.0,原來只是聽說EventBus的鼎鼎大名,一直沒仔細研究過。趁著周末有些時間,研究下代碼,也算沒有虛度光陰。

EventBus GitHub : https://github.com/greenrobot/EventBus

EventBus3.0簡介

EventBus是greenrobot出品的一個用於Android中事件發布/訂閱的庫。以前傳遞對象可能通過接口、廣播、文件等等,尤其像同一個Activity兩個Fragment之間采用接口傳遞對象,十分的麻煩,而且耦合度較高。使用EventBus之後,這些將不再是問題。盜用GiHub上EventBus的一張圖。
eventbusvcq9vPvPws7EoaM8L3A+DQo8aDEgaWQ9"eventbus30的使用">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();
    }
}

MainActivityonCreate()/onDestroy()中分別注冊/反注冊EventBus。然後寫了四個測試ThreadMode的方法,調用時機注釋的很清楚,就不贅述了。最後在SecondActivity的主線程和子線程中分別調用Post()方法,注意,這裡Post()方法的參數為Object類型,這也就意味著我們傳遞任何對象都是可以的,例如JavaBeanList等等都是可以的,這裡為了方便演示直接傳遞了String。Log信息如下:
main

child
第一張圖中發布者發送線程為主線程,即Post thread = 1,在訂閱者收到消息時,ThreadMode = MainThreadMode = Posting的方法都在主線程調用,ASYNCBACKGROUND都新開了線程。圖二對比注釋同理。

除此之外,Subscribe注解還支弛喎?/kf/yidong/wp/" target="_blank" class="keylink">WPGNvZGU+cHJpb3JpdHk8L2NvZGU+us08Y29kZT5zdGlja3k8L2NvZGU+yvTQ1KGjPGNvZGU+cHJpb3JpdHk8L2NvZGU+yejWw73TytXV37XE08XPyLy2o6zErMjP1rXOqjCho9PFz8i8trjftcS3vbeoz8ixu7X308OjrNTat723qLX308PN6rPJuvO/ydLUtffTwzxjb2RlPkV2ZW50QnVzLmdldERlZmF1bHQoKS5jYW5jZWxFdmVudERlbGl2ZXJ5KGV2ZW50KSA7PC9jb2RlPtbV1rnTxc/IvLa1zbXEt723qLXEtffTw6GjPGNvZGU+c3RpY2t5PC9jb2RlPs6q1bPQ1MrCvP6jrMSsyM/OqrnYsdXXtMysoaPE3Lm7ytW1vbap1MTWrsewt6LLzbW9tcTX7rrz0rvM9c/7z6KjrLKix9K3osvNtcS3vbeosrvU2crHPGNvZGU+cG9zdCgpPC9jb2RlPrb4ysc8Y29kZT5wb3N0U3RpY2t5KCk8L2NvZGU+oaM8L3A+DQo8aDEgaWQ9"eventbus30源碼解析">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為枚舉類型,默認值為POSTINGsticky()默認返回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 subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
    synchronized (this) {
        for (SubscriberMethod subscriberMethod : subscriberMethods) {
            subscribe(subscriber, subscriberMethod);
        }
    }
}

首先得到訂閱者的報名.類名,即哪個具體類注冊。然後調用subscriberMethodFinder.findSubscriberMethods(subscriberClass),從方法名和返回值來看,findSubscriberMethods()的作用應該是遍歷查找訂閱者中所有的訂閱方法。

SubscriberMethodFinder#findSubscriberMethods()

List findSubscriberMethods(Class subscriberClass) {
    // 查找緩存
    List 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 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 subscriberMethods = new ArrayList<>();
    ...
}

private List getMethodsAndRelease(FindState findState) {
    List 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 subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
    synchronized (this) {
        for (SubscriberMethod subscriberMethod : subscriberMethods) {
            subscribe(subscriber, subscriberMethod);
        }
    }
}

調用SubscriberMethodFinder#findSubscriberMethods()後,以List形式返回了訂閱者所有的訂閱事件。然後遍歷執行subscribe()方法。看樣子應該是遍歷List,然後將訂閱者和訂閱事件綁定。沒撒好說的,跟進subscribe()

EventBus#subscribe()

private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
    // 獲取訂閱事件的類型,即訂閱方法中的唯一參數類型
    Class eventType = subscriberMethod.eventType;
    // 用訂閱者和訂閱方法構造一個Subscription對象
    Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
    // 查找所有的訂閱了訂閱事件的訂閱者
    CopyOnWriteArrayList subscriptions = subscriptionsByEventType.get(eventType);
    // 沒有訂閱者訂閱過則新建個CopyOnWriteArrayList,並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> 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).
            Set, Object>> entries = stickyEvents.entrySet();
            for (Map.Entry, 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

上面代碼中currentPostingThreadStateThreadLocal對象,對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> 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> lookupAllEventTypes(Class eventClass) {
    synchronized (eventTypesCache) {
        // 根據訂閱事件查找所有自身及父類,eventTypes默認為null
        List> 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 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> 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 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

現在要反注冊咯。移除相應的keyvalue即可。EventBus3.0的使用及源碼解析到此結束,Have a nice day~

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved