Android—EventBus使用與源碼分析

EventBus

安卓事件發佈/訂閱框架

事件傳遞既可用於Android四大組件間通訊

EventBus的優點是代碼簡潔,使用簡單,並將事件發佈和訂閱充分解耦

在onStart進行註冊,onStop進行註銷。

implementation 'org.greenrobot:eventbus:3.1.1'

使用:

作爲事件的發佈者,需要定義所發佈的事件的類:

 public class MessageEvent{
    public final String message;
    public static MessageEvent getInstance(String message) {
        return new MessageEvent(message);
    }
    private MessageEvent(String message) {
        this.message = message;
    }
  }

聲明和註釋訂閱方法,選擇指定線程模式

    @Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
    public void onGetMessage(MessageEvent message) {
       Log.e("....",message.message)
    }

發送消息

 EventBus.getDefault().post(new EventMessage("這是我使用EventBus發送消息"))

Android引入了StickyBroadcast,在廣播發送結束後會保存剛剛發送的廣播(Intent),這樣當接收者註冊完 Receiver後就可以接收到剛纔已經發布的廣播,通過註解的方式設置sticky爲true,那麼事件處理函數則可以處理上一次的事件對象。

事件訂閱者可以通過註解的方式選擇處理事件的方法所在的線程:

  1. POSTING:默認,表示事件處理函數的線程跟發佈事件的線程在同一個線程。
  2. MAIN:表示事件處理函數的線程在主線程(UI)線程,因此在這裏不能進行耗時操作。
  3. BACKGROUND:表示事件處理函數的線程在後臺線程,因此不能進行UI操作。如果發佈事件的線程是主線程(UI線程),那麼事件處理函數將會開啓一個後臺線程,如果果發佈事件的線程是在後臺線程,那麼事件處理函數就使用該線程。
  4. ASYNC:表示無論事件發佈的線程是哪一個,事件處理函數始終會新建一個子線程運行,同樣不能進行UI操作。

訂閱者同時需要在總線上註冊和註銷自己

    override fun onStart() {
        super.onStart()
        EventBus.getDefault().register(this)
    }

    override fun onStop() {
        super.onStop()
        EventBus.getDefault().unregister(this)
    }

發送事件

    btn.setOnClickListener { 
        EventBus.getDefault().post(MessageEvent.getInstance("發送消息"))
    }

源碼分析

註冊發送消息都要getDefault(),現在進入到getDefault方法裏

    /** Convenience singleton for apps using a process-wide EventBus instance. */
    public static EventBus getDefault() {
        if (defaultInstance == null) {
            synchronized (EventBus.class) {
                if (defaultInstance == null) {
                    defaultInstance = new EventBus();
                }
            }
        }
        return defaultInstance;
    }

//將DEFAULT_BUILDER傳入EventBus的構造函數
    public EventBus() {
        this(DEFAULT_BUILDER);
    }

    private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();

單例模式,運用到雙重檢查鎖,只有一個defaultInstance。

所以getDefault方法最後是返回一個傳入EventBusBuilder的EventBus對象,採用建造者模式。

我們現看看EvenBus類的註冊register方法

    public void register(Object subscriber) {
//獲取上下文,我們傳入的註冊者是activity
        Class<?> subscriberClass = subscriber.getClass();
//根據上下文找訂閱方法
        List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
//將註冊者和註冊事件綁定起來
        synchronized (this) {
            for (SubscriberMethod subscriberMethod : subscriberMethods) {
                subscribe(subscriber, subscriberMethod);
            }
        }
    }


    List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
//在緩存中查找class對象的訂閱方法列表
        List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
        if (subscriberMethods != null) {
            return subscriberMethods;
        }
        if (ignoreGeneratedIndex) {
//通過反射機制得到訂閱者類class對象對應的訂閱事件方法列表
            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 {
//緩存此class對象的訂閱方法列表
            METHOD_CACHE.put(subscriberClass, subscriberMethods);
            return subscriberMethods;
        }
    }

接下來看看subscribe方法

    // Must be called in synchronized block
    private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
        Class<?> eventType = subscriberMethod.eventType;
//將class對象和其訂閱方法列表封裝成Subscription對象,這裏纔是真正的綁定。
        Subscription newSubscription = new Subscription(subscriber, subscriberMethod);

//subscriptions存放多個Subscription對象,意味着存放多個class對象和各個對象對應的訂閱方法。
//且這些class對象的訂閱方法的EventType相同。
//subscriptionsByEventType隊列又存放着多個subscriptions對象。
//通過訂閱方法的eventType屬性得到隊列中的subscriptions對象。
        CopyOnWriteArrayList<Subscription> subscriptions = 
subscriptionsByEventType.get(eventType);
        if (subscriptions == null) {
            subscriptions = new CopyOnWriteArrayList<>();
            subscriptionsByEventType.put(eventType, subscriptions);
        } else {
//如果這個對象已經在subscriptions列表當中,則拋出異常,已經註冊過了
            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++) {
            if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
                subscriptions.add(i, newSubscription);
                break;
            }
        }

//通過訂閱者class對象得到其對應的訂閱方法的EventType列表。
        List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
        if (subscribedEvents == null) {
            subscribedEvents = new ArrayList<>();
            typesBySubscriber.put(subscriber, subscribedEvents);
        }
//添加EventType到class對象對應的EventType列表當中
        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);
            }
        }
    }

    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, isMainThread());
        }
    }

最後調用的是 postToSubscription方法,這個方法到後面再分析。

register方法總結:

  1. 遍歷傳入的class對象找到所有onEvent以開頭訂閱方法,然後一起封裝成Subscription對象。
  2. 判斷是否註冊過該EventType,有則按照優先級將Subscription對象裝入Subscriptions隊列。
  3. 沒有就新建Subscriptions隊列,並把Subscriptions隊列加入到subscriptionsByEventType<EventType,List<訂閱這個EventType的訂閱者>>隊列的Value的List中。
  4. 再將訂閱方法的EventType存入到typesBySubscriber<訂閱者,List<訂閱的EventType>>隊列的Value的List中。
  5. 最後判斷是否爲粘性事件,是則將黏性事件發送到指定的訂閱方法當中並根據threadMode進行相應處理。

下面到EvenBus的Post方法

/** Posts the given event to the event bus. */
    public void post(Object event) {
//通過ThreadLocal機制得到當前線程的postingState對象,線程獨有的,不會共享線程數據
        PostingThreadState postingState = currentPostingThreadState.get();
//獲取事件隊列
        List<Object> eventQueue = postingState.eventQueue;
//在此線程的eventQueue中添加此事件對象
        eventQueue.add(event);

        if (!postingState.isPosting) {
//判斷當前線程是否UI線程
            postingState.isMainThread = isMainThread();
            postingState.isPosting = true;
            if (postingState.canceled) {
                throw new EventBusException("Internal error. Abort state was not reset");
            }
            try {
//遍歷此線程消息隊列,處理消息隊列中的消息事件
//每次執行postSingleEvent()都會從隊列中取出一個事件,這樣不停循環取出事件處理,直到隊列全部取完。 
                while (!eventQueue.isEmpty()) {
                    postSingleEvent(eventQueue.remove(0), postingState);
                }
            } finally {
                postingState.isPosting = false;
                postingState.isMainThread = false;
            }
        }
    }

ThreadLocal是一個線程內部的數據存儲類,通過它可以在指定的線程中存儲數據,而這段數據是不會與其他線程共享的。其內部原理是通過生成一個它包裹的泛型對象的數組,在不同的線程會有不同的數組索引值,通過這樣就可以做到每個線程通過 get() 方法獲取的時候,取到的只能是自己線程所對應的數據。 

核心方法是postSingleEvent

    private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
        Class<?> eventClass = event.getClass();
        boolean subscriptionFound = false;
//eventInheritance表示一個子類事件能否響應父類的onEvent() 方法
        if (eventInheritance) {
//獲取到eventClass所有父類的集合
            List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
            int countTypes = eventTypes.size();
            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));
            }
        }
    }

lookupAllEventTypes()它通過循環和遞歸一起用,將一個類的父類,接口,父類的接口,父類接口的父類,全部添加到全局靜態變量集合eventTypes中。之所以用全局靜態變量的好處在於用全局靜態變量只需要將那耗時又複雜的循環+遞歸方法執行一次就夠了,下次只需要通過 key:事件類名來判斷這個事件是否以及執行過 lookupAllEventTypes() 方法。

然後我們繼續往下,看發送方法 postSingleEventForEventType()

    private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
        CopyOnWriteArrayList<Subscription> subscriptions;
        synchronized (this) {
//所有訂閱了eventClass的事件集合
            subscriptions = subscriptionsByEventType.get(eventClass);
        }
        if (subscriptions != null && !subscriptions.isEmpty()) {
//回調subscription的響應方法
            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;
    }

跟註冊的粘性事件一樣,最後同樣是在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);
        }
    }

對聲明不同線程模式的事件做不同的響應方法,最終都是通過invokeSubscriber()反射訂閱者類中的以onEvent開頭的方法以執行。

Post方法總結:

  1. 取當前線程的發送事件封裝數據,並從封裝的數據中拿到發送事件的事件隊列。
  2. 將要發送的事件加入到事件隊列中去。
  3. 循環,每次發送隊列中的一條事件給所有訂閱了這個EventType的訂閱者。
  4. 如果是子事件可以響應父事件的事件模式,需要先將這個事件的所有父類、接口、父類的接口、父類接口的父類都找到,並讓訂閱了這些父類信息的訂閱者也都響應這條事件。

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 {
            logger.log(Level.WARNING, "Subscriber to unregister was not registered before: " + subscriber.getClass());
        }
    }

    /** Only updates subscriptionsByEventType, not typesBySubscriber! Caller must update typesBySubscriber. */
    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--;
                }
            }
        }
    }

Unregister方法總結:

  • 根據subscriber獲取他的EventType列表。
  • 再根據EventType列表,從subscriptionsByEventType列表中獲取subscriber們和他們對應訂閱方法的集合subscriptions。
  • 最後remove掉subscriptions集合中的subscriber以達到註冊效果。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章