Android -- EventBus解析


EventBus

EventBus 是一個 Android 事件發布/訂閱框架,通過解耦發布者和訂閱者簡化 Android 事件傳遞。傳統的事件傳遞方式包括:Handler、BroadCastReceiver、Interface 回調,相比之下 EventBus 的優點是代碼簡潔,使用簡單,並將事件發布和訂閱充分解耦。

事件(Event):又可稱為消息。其實就是一個對象,可以是網絡請求返回的字符串,也可以是某個開關狀態等等。事件類型(EventType)指事件所屬的 Class。事件分為一般事件和 Sticky 事件,相對於一般事件,Sticky 事件不同之處在於,當事件發布后,再有訂閱者開始訂閱該類型事件,依然能收到該類型事件最近一個 Sticky 事件。

訂閱者(Subscriber):訂閱某種事件類型的對象。當有發布者發布這類事件后,EventBus 會執行訂閱者的 onEvent 函數,這個函數叫事件響應函數。訂閱者通過 register 接口訂閱某個事件類型,unregister 接口退訂。訂閱者存在優先級,優先級高的訂閱者可以取消事件繼續向優先級低的訂閱者分發,默認所有訂閱者優先級都為 0。

發布者(Publisher):發布某事件的對象,通過 post 接口發布事件。

類關系

流程

EventBus 負責存儲訂閱者、事件相關信息,訂閱者和發布者都只和 EventBus 關聯。

訂閱者首先調用 EventBus 的 register 接口訂閱某種類型的事件,當發布者通過 post 接口發布該類型的事件時,EventBus 執行調用者的事件響應函數。

解析

EventBus 類負責所有對外暴露的 API,其中的 register()、post()、unregister() 函數配合上自定義的 EventType 及事件響應函數即可完成核心功能。

EventBus 默認可通過靜態函數 getDefault() 獲取單例,當然有需要也可以通過 EventBusBuilder 或 構造函數新建一個 EventBus,每個新建的 EventBus 發布和訂閱事件都是相互隔離的,即一個 EventBus 對象中的發布者發布事件,另一個 EventBus 對象中的訂閱者不會收到該訂閱。

EventBus.getDefault().register(this);

register()方法解析:

    public void register(Object subscriber) {
        Class<?> subscriberClass = subscriber.getClass();
        // @Subscribe in anonymous classes is invisible to annotation processing, always fall back to reflection
        boolean forceReflection = subscriberClass.isAnonymousClass();
        List<SubscriberMethod> subscriberMethods =
                subscriberMethodFinder.findSubscriberMethods(subscriberClass, forceReflection);//主要查找又什么方法是在這個函數里面
        for (SubscriberMethod subscriberMethod : subscriberMethods) {
            subscribe(subscriber, subscriberMethod);
        }
    }

通過一個findSubscriberMethods方法找到了一個訂閱者中的所有訂閱方法,返回一個 List

List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass, boolean forceReflection) {
  		//類名
        String key = subscriberClass.getName();
        List<SubscriberMethod> subscriberMethods;
        synchronized (METHOD_CACHE) {
          	//判斷是否有緩存,有緩存直接返回緩存  
            subscriberMethods = METHOD_CACHE.get(key);
        }
  		//第一次進來subscriberMethods肯定是Null
        if (subscriberMethods != null) {
            return subscriberMethods;
        }
  		//INDEX是GeneratedSubscriberIndex
        if (INDEX != null && !forceReflection) {
            subscriberMethods = findSubscriberMethodsWithIndex(subscriberClass);//后面再來看這個函數
            if (subscriberMethods.isEmpty()) {
                subscriberMethods = findSubscriberMethodsWithReflection(subscriberClass);//通過反射來找到方法
            }
        } else {
            subscriberMethods = findSubscriberMethodsWithReflection(subscriberClass);//通過反射來找到方法
        }
        if (subscriberMethods.isEmpty()) {
            throw new EventBusException("Subscriber " + subscriberClass
                    + " and its super classes have no public methods with the @Subscribe annotation");
        } else {
            synchronized (METHOD_CACHE) {
                METHOD_CACHE.put(key, subscriberMethods);//放入緩存
            }
            return subscriberMethods;
        }
    }

findSubscriberMethodsWithReflection:

private List<SubscriberMethod> findSubscriberMethodsWithReflection(Class<?> subscriberClass) {
        List<SubscriberMethod> subscriberMethods = new ArrayList<SubscriberMethod>();
        Class<?> clazz = subscriberClass;
        HashSet<String> eventTypesFound = new HashSet<String>();
        StringBuilder methodKeyBuilder = new StringBuilder();
        while (clazz != null) {
            String name = clazz.getName();
          	//過濾掉系統類  
            if (name.startsWith("java.") || name.startsWith("javax.") || name.startsWith("android.")) {
                // Skip system classes, this just degrades performance
                break;
            }

            // Starting with EventBus 2.2 we enforced methods to be public (might change with annotations again)
         	 //通過反射,獲取到訂閱者的所有方法
            Method[] methods = clazz.getDeclaredMethods();
            for (Method method : methods) {
                int modifiers = method.getModifiers();
              	//判斷是否是public,是否有修飾符
                if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
                  	//獲得訂閱函數的參數 
                    Class<?>[] parameterTypes = method.getParameterTypes();
                  	//參數只能是1個
                    if (parameterTypes.length == 1) {
                      	//通過Annotation去拿一些數據
                        Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
                        if (subscribeAnnotation != null) {
                            String methodName = method.getName();//方法名字
                            Class<?> eventType = parameterTypes[0];//類型
                          	//獲取參數類型,其實就是接收事件的類型  
                            methodKeyBuilder.setLength(0);
                            methodKeyBuilder.append(methodName);
                            methodKeyBuilder.append('>').append(eventType.getName());

                            String methodKey = methodKeyBuilder.toString();
                            if (eventTypesFound.add(methodKey)) {
                                // Only add if not already found in a sub class
                                ThreadMode threadMode = subscribeAnnotation.threadMode();
                              	//封裝一個訂閱方法對象,這個對象包含Method對象,threadMode對象,eventType對象,優先級prority,sticky
                                subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
                                        subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
                            }
                        }
                    } else if (strictMethodVerification) {
                        if (method.isAnnotationPresent(Subscribe.class)) {
                            String methodName = name + "." + method.getName();
                            throw new EventBusException("@Subscribe method " + methodName +
                                    "must have exactly 1 parameter but has " + parameterTypes.length);
                        }
                    }
                } else if (strictMethodVerification) {
                    if (method.isAnnotationPresent(Subscribe.class)) {
                        String methodName = name + "." + method.getName();
                        throw new EventBusException(methodName +
                                " is a illegal @Subscribe method: must be public, non-static, and non-abstract");
                    }

                }
            }
			//再去查找父類
            clazz = clazz.getSuperclass();
        }
        return subscriberMethods;
    }

方法的講解都在注釋里面。

for (SubscriberMethod subscriberMethod : subscriberMethods) {
            subscribe(subscriber, subscriberMethod);
        }

每個訂閱方法都調用subscribe方法:

private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
  		//從訂閱方法中拿到訂閱事件的類型  
        Class<?> eventType = subscriberMethod.eventType;
  		//通過訂閱事件類型,找到所有的訂閱(Subscription),訂閱中包含了訂閱者,訂閱方法
        CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
  		//創建一個新的訂閱 
        Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
  		//將新建的訂閱加入到這個事件類型對應的所有訂閱列表  
        if (subscriptions == null) {
          	//如果該事件目前沒有訂閱列表,那么創建並加入該訂閱  
            subscriptions = new CopyOnWriteArrayList<Subscription>();
            subscriptionsByEventType.put(eventType, subscriptions);
        } else {
          	//如果有訂閱列表,檢查是否已經加入過  
            if (subscriptions.contains(newSubscription)) {
                throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
                        + eventType);
            }
        }

        // Starting with EventBus 2.2 we enforced methods to be public (might change with annotations again)
        // subscriberMethod.method.setAccessible(true);

        // Got to synchronize to avoid shifted positions when adding/removing concurrently
  		//根據優先級插入訂閱
        synchronized (subscriptions) {
            int size = subscriptions.size();
            for (int i = 0; i <= size; i++) {
                if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
                    subscriptions.add(i, newSubscription);
                    break;
                }
            }
        }
		//將這個訂閱事件加入到訂閱者的訂閱事件列表中
        List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
        if (subscribedEvents == null) {
            subscribedEvents = new ArrayList<Class<?>>();
            typesBySubscriber.put(subscriber, subscribedEvents);
        }
        subscribedEvents.add(eventType);

        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);
            }
        }
    }

第一步:通過subscriptionsByEventType得到該事件類型所有訂閱者信息隊列,根據優先級將當前訂閱者信息插入到訂閱者隊列subscriptionsByEventType中;

第二步:在typesBySubscriber中得到當前訂閱者訂閱的所有事件隊列,將此事件保存到隊列typesBySubscriber中,用於后續取消訂閱;

第三步:檢查這個事件是否是 Sticky 事件,如果是則從stickyEvents事件保存隊列中取出該事件類型最后一個事件發送給當前訂閱者

private void checkPostStickyEventToSubscription(Subscription newSubscription, Object stickyEvent) {
        if (stickyEvent != null) {
            // If the subscriber is trying to abort the event, it will fail (event is not tracked in posting state)
            // --> Strange corner case, which we don't take care of here.
            postToSubscription(newSubscription, stickyEvent, Looper.getMainLooper() == Looper.myLooper());
        }
    }
private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
        switch (subscription.subscriberMethod.threadMode) {
            case PostThread:
                invokeSubscriber(subscription, event);
                break;
            case MainThread:
                if (isMainThread) {
                    invokeSubscriber(subscription, event);
                } else {
                    mainThreadPoster.enqueue(subscription, event);
                }
                break;
            case BackgroundThread:
                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);
        }
    }

過程:

  1. 找到被注冊者中所有的訂閱方法。
  2. 依次遍歷訂閱方法,找到EventBus中eventType對應的訂閱列表,然后根據當前訂閱者和訂閱方法創建一個新的訂閱加入到訂閱列表。
  3. 找到EvnetBus中subscriber訂閱的事件列表,將eventType加入到這個事件列表。
public void post(Object event) {
  		//拿到PostingThreadState
        PostingThreadState postingState = currentPostingThreadState.get();
        List<Object> eventQueue = postingState.eventQueue;
  		//將事件放入隊列 
        eventQueue.add(event);

        if (!postingState.isPosting) {
            postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();
            postingState.isPosting = true;//設置為正在post
            if (postingState.canceled) {
                throw new EventBusException("Internal error. Abort state was not reset");
            }
            try {
                while (!eventQueue.isEmpty()) {
                  	//分發事件
                    postSingleEvent(eventQueue.remove(0), postingState);
                }
            } finally {
                postingState.isPosting = false;
                postingState.isMainThread = false;
            }
        }
    }
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
        Class<?> eventClass = event.getClass();
        boolean subscriptionFound = false;
        if (eventInheritance) {
          	//找到eventClass對應的事件,包含父類對應的事件和接口對應的事件 
            List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
            int countTypes = eventTypes.size();
            for (int h = 0; h < countTypes; h++) {
                Class<?> clazz = eventTypes.get(h);
              	//postSingleEventForEventType去查找,其中里面的數據都是通過subscribe()緩存進去的
                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一個NoSubscriberEvent事件
                post(new NoSubscriberEvent(this, event));
            }
        }
    }
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
        CopyOnWriteArrayList<Subscription> subscriptions;
        synchronized (this) {
          	//subscriptionsByEventType是從subscribe()緩存進去的
            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 {
                    postingState.event = null;
                    postingState.subscription = null;
                    postingState.canceled = false;
                }
                if (aborted) {
                    break;
                }
            }
            return true;
        }
        return false;
    }

post 函數會首先得到當前線程的 post 信息PostingThreadState,其中包含事件隊列,將當前事件添加到其事件隊列中,然后循環調用 postSingleEvent 函數發布隊列中的每個事件。

postSingleEvent 函數會先去eventTypesCache得到該事件對應類型的的父類及接口類型,沒有緩存則查找並插入緩存。循環得到的每個類型和接口,調用 postSingleEventForEventType 函數發布每個事件到每個訂閱者。

postSingleEventForEventType 函數在subscriptionsByEventType查找該事件訂閱者訂閱者隊列,調用 postToSubscription 函數向每個訂閱者發布事件。

postToSubscription 函數中會判斷訂閱者的 ThreadMode,從而決定在什么 Mode 下執行事件響應函數。

ThreadMode

  1. PostThread:默認的 ThreadMode,表示在執行 Post 操作的線程直接調用訂閱者的事件響應方法,不論該線程是否為主線程(UI 線程)。當該線程為主線程時,響應方法中不能有耗時操作,否則有卡主線程的風險。適用場景:對於是否在主線程執行無要求,但若 Post 線程為主線程,不能耗時的操作
  2. MainThread:在主線程中執行響應方法。如果發布線程就是主線程,則直接調用訂閱者的事件響應方法,否則通過主線程的 Handler 發送消息在主線程中處理——調用訂閱者的事件響應函數。顯然,MainThread類的方法也不能有耗時操作,以避免卡主線程。適用場景:必須在主線程執行的操作
  3. BackgroundThread:在后台線程中執行響應方法。如果發布線程不是主線程,則直接調用訂閱者的事件響應函數,否則啟動唯一的后台線程去處理。由於后台線程是唯一的,當事件超過一個的時候,它們會被放在隊列中依次執行,因此該類響應方法雖然沒有PostThread類和MainThread類方法對性能敏感,但最好不要有重度耗時的操作或太頻繁的輕度耗時操作,以造成其他操作等待。適用場景:操作輕微耗時且不會過於頻繁,即一般的耗時操作都可以放在這里;
  4. Async:不論發布線程是否為主線程,都使用一個空閑線程來處理。和BackgroundThread不同的是,Async類的所有線程是相互獨立的,因此不會出現卡線程的問題。適用場景:長耗時操作,例如網絡訪問

我是天王蓋地虎的分割線

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'de.greenrobot:eventbus:3.0.0-beta1'
}

Android -- EventBus使用: http://www.cnblogs.com/yydcdut/p/4290846.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM