EventBus3.1.1 源碼解析

前言

在上一文我介紹了EventBus的基礎(chǔ)知識(shí)以及如何使用EventBus3.0+ 使用入門(mén)挑势,但EventBus是如何工作的潮饱?還尚且不知香拉,今天就來(lái)一探究竟凫碌!凡事都有先后順序盛险,分析代碼也一樣,我們?cè)谑褂肊ventBus的時(shí)候一般是按照以下順序執(zhí)行:
?1楔壤、定義事件
?2蹲嚣、訂閱事件
?3端铛、解除訂閱
那我也按照這個(gè)順序進(jìn)行解析禾蚕。

定義事件

其實(shí)這個(gè)沒(méi)什么好分析换淆,簡(jiǎn)單的講就是定義一個(gè)Bean對(duì)象倍试,用來(lái)發(fā)送所需的數(shù)據(jù)县习。

訂閱事件

@Override
protected void onCreate(Bundle savedInstanceState) {           
     super.onCreate(savedInstanceState);
     setContentView(R.layout.activity_main)躁愿;
     // 注冊(cè)事件
     EventBus.getDefault().register(this)彤钟;
}

在項(xiàng)目使用中逸雹,我們都是在Activty或者Fragmengt的onCreate中通過(guò)EventBus.getDefault().register(this)注冊(cè)事件梆砸,那便從getDefault()開(kāi)始帖世,一探究竟狮暑。

1搬男、EventBus#getDefault()

static volatile EventBus defaultInstance;

public static EventBus getDefault() {
    EventBus instance = defaultInstance;
    if (instance == null) {
        synchronized (EventBus.class) {
            instance = EventBus.defaultInstance;
            if (instance == null) {
                instance = EventBus.defaultInstance = new EventBus();
            }
        }
    }
    return instance;
}

這是我們?cè)偈煜げ贿^(guò)的采用雙重校驗(yàn)并加鎖的單例模式备埃,volatile關(guān)鍵字是為了防止處理器亂序執(zhí)行而導(dǎo)致DCL(雙重校驗(yàn)并加鎖)失效的問(wèn)題按脚。

public EventBus() {
    this(DEFAULT_BUILDER);
}

EventBus(EventBusBuilder builder) {
    logger = builder.getLogger();
    subscriptionsByEventType = new HashMap<>();
    typesBySubscriber = new HashMap<>();
    stickyEvents = new ConcurrentHashMap<>();
    mainThreadSupport = builder.getMainThreadSupport();
    mainThreadPoster = mainThreadSupport != null ? mainThreadSupport.createPoster(this) : null;
    backgroundPoster = new BackgroundPoster(this);
    asyncPoster = new AsyncPoster(this);
    indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0;
    subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
    builder.strictMethodVerification, builder.ignoreGeneratedIndex);
    logSubscriberExceptions = builder.logSubscriberExceptions;
    logNoSubscriberMessages = builder.logNoSubscriberMessages;
    sendSubscriberExceptionEvent = builder.sendSubscriberExceptionEvent;
    sendNoSubscriberEvent = builder.sendNoSubscriberEvent;
    throwSubscriberException = builder.throwSubscriberException;
    eventInheritance = builder.eventInheritance;
    executorService = builder.executorService;
}

在構(gòu)造方法中主要做了一些變量的初始化。我們還是主要看一下在register中EventBus做了哪些操作脖旱。

2堪遂、EventBus#register()

public void register(Object subscriber) {
    //  通過(guò)我們傳入的訂閱者對(duì)象,獲取訂閱者class類(lèi)的實(shí)例萌庆。
    Class<?> subscriberClass = subscriber.getClass();
    List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
    synchronized (this) {
        for (SubscriberMethod subscriberMethod : subscriberMethods) {
            subscribe(subscriber, subscriberMethod);
        }
    }
}

由上述代碼可以看出溶褪,EventBus通過(guò)傳入的訂閱者實(shí)例獲取訂閱者class類(lèi)的對(duì)象。并通過(guò)SubscriberMethodFinder類(lèi)查找出這個(gè)class對(duì)象中所有訂閱的方法践险。

3猿妈、SubscriberMethodFinder#findSubscriberMethods()

private static final Map<Class<?>, List<SubscriberMethod>> METHOD_CACHE = new
            ConcurrentHashMap<>();
            
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
    List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
    if (subscriberMethods != null) {
        return subscriberMethods;
    }

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

METHOD_CACHE是用來(lái)緩存訂閱方法的巍虫,key就是傳入的訂閱者class類(lèi)的對(duì)象彭则。如果緩存中有則直接獲取并返回。
如果是第一次創(chuàng)建垫言,因?yàn)镋ventBus中指定ignoreGeneratedIndex默認(rèn)為false贰剥,則會(huì)進(jìn)入到findUsingInfo()這個(gè)方法中。

4筷频、SubscriberMethodFinder#findUsingInfo()

private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
    //  4.1
    FindState findState = prepareFindState();
    //  4.2
    findState.initForSubscriber(subscriberClass);
    while (findState.clazz != null) {
        //  4.3
        findState.subscriberInfo = getSubscriberInfo(findState);
        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);
        }
        //  4.4 循環(huán)查找父類(lèi),如果遇到系統(tǒng)類(lèi)時(shí)會(huì)跳過(guò)前痘,提高查找速度凛捏,提升性能
        findState.moveToSuperclass();
    }
    return getMethodsAndRelease(findState);
}

其中FindState是一個(gè)內(nèi)部類(lèi),用來(lái)保存訂閱者類(lèi)相關(guān)的信息芹缔。

4.1坯癣、SubscriberMethodFinder#prepareFindState()
private static final FindState[] FIND_STATE_POOL = new FindState[POOL_SIZE];

private FindState prepareFindState() {
    synchronized (FIND_STATE_POOL) {
        for (int i = 0; i < POOL_SIZE; i++) {
            FindState state = FIND_STATE_POOL[i];
            if (state != null) {
                FIND_STATE_POOL[i] = null;
                return state;
            }
        }
    }
    return new FindState();
}

prepareFindState()用來(lái)創(chuàng)建FindState對(duì)象,首先從FindState池中獲取最欠,如果有則直接返回示罗,并且將該下標(biāo)的對(duì)象置空(當(dāng)獲取到訂閱方法并且開(kāi)始發(fā)布時(shí)又會(huì)重新賦值)惩猫,沒(méi)有就重新new一個(gè)FindState對(duì)象。

4.2蚜点、SubscriberMethodFinder#FindState#initForSubscriber()
static class FindState {
    final List<SubscriberMethod> subscriberMethods = new ArrayList<>();
    final Map<Class, Object> anyMethodByEventType = new HashMap<>();
    final Map<String, Class> subscriberClassByMethodKey = new HashMap<>();
    final StringBuilder methodKeyBuilder = new StringBuilder(128);

    Class<?> subscriberClass;
    Class<?> clazz;
    boolean skipSuperClasses;
    SubscriberInfo subscriberInfo;

    void initForSubscriber(Class<?> subscriberClass) {
        this.subscriberClass = clazz = subscriberClass;
        skipSuperClasses = false;
        subscriberInfo = null;
    }

...

}

初始化FindState對(duì)象轧房。
?1. subscriberMethods保存SubscriberMethod對(duì)象的列表绍绘。

4.3奶镶、SubscriberMethodFinder#getSubscriberInfo
private SubscriberInfo getSubscriberInfo(FindState findState) {
    if (findState.subscriberInfo != null && findState.subscriberInfo.getSuperSubscriberInfo()
            != null) {
        SubscriberInfo superclassInfo = findState.subscriberInfo.getSuperSubscriberInfo();
        if (findState.clazz == superclassInfo.getSubscriberClass()) {
            return superclassInfo;
        }
    }
    if (subscriberInfoIndexes != null) {
        for (SubscriberInfoIndex index : subscriberInfoIndexes) {
            SubscriberInfo info = index.getSubscriberInfo(findState.clazz);
            if (info != null) {
                return info;
            }
        }
    }
    return null;
}

這個(gè)方法主要是找到訂閱者信息是否存在,如果存在就直接返回陪拘,第一次創(chuàng)建返回null厂镇。所以上面的findUsingInfo會(huì)走到else中,并調(diào)用findUsingReflectionInSingleClass()方法左刽。

4.4 SubscriberMethodFinder#FindState#moveToSuperclass()
void moveToSuperclass() {
    if (skipSuperClasses) {
        clazz = null;
    } else {
        clazz = clazz.getSuperclass();
        String clazzName = clazz.getName();
        /** Skip system classes, this just degrades performance. */
        if (clazzName.startsWith("java.") || clazzName.startsWith("javax.") || clazzName
                .startsWith("android.")) {
            clazz = null;
        }
    }
}

5捺信、SubscriberMethodFinder#findUsingReflectionInSingleClass

private void findUsingReflectionInSingleClass(FindState findState) {
    Method[] methods;
    try {
        // This is faster than getMethods, especially when subscribers are fat classes like
        // Activities
        //  找到訂閱者類(lèi)中所有的方法
        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值
        int modifiers = method.getModifiers();
        if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
            //  返回方法的參數(shù)類(lèi)型數(shù)組
            Class<?>[] parameterTypes = method.getParameterTypes();
            //  如果參數(shù)只有1個(gè)
            if (parameterTypes.length == 1) {
                //  獲取該方法的注解
                Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
                //  如果包含@Subscribe注解
                if (subscribeAnnotation != null) { 
                //  獲取訂閱方法的事件類(lèi)型
                Class<?> eventType = parameterTypes[0];
                    if (findState.checkAdd(method, eventType)) {
                        //  獲取訂閱方法的線(xiàn)程模型
                        ThreadMode threadMode = subscribeAnnotation.threadMode();
                        //  5.1 保存訂閱者信息
                        findState.subscriberMethods.add(new SubscriberMethod(method,
                                eventType, threadMode,
                                subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
                    }
                }
            } 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");
        }
    }
}

這個(gè)方法的主要是作用是通過(guò)反射找出訂閱者類(lèi)中所有的訂閱方法,并保存訂閱者的相關(guān)信息到FindState對(duì)象的subscriberMethods列表中欠痴,包括訂閱方法残黑、事件類(lèi)型、線(xiàn)程模型斋否、優(yōu)先級(jí)梨水、粘黏性。

5.1 SubscriberMethod#SubscriberMethod()
public class SubscriberMethod {
    final Method method;
    final ThreadMode threadMode;
    final Class<?> eventType;
    final int priority;
    final boolean sticky;
    /** Used for efficient comparison */
    String methodString;

    public SubscriberMethod(Method method, Class<?> eventType, ThreadMode threadMode, int priority, boolean sticky) {
        this.method = method;
        this.threadMode = threadMode;
        this.eventType = eventType;
        this.priority = priority;
        this.sticky = sticky;
    }
    
    ...
    
}

至此茵臭,當(dāng)findUsingInfo的循環(huán)結(jié)束時(shí)疫诽,通過(guò)Register()傳進(jìn)來(lái)的訂閱者類(lèi)的對(duì)象,查找到訂閱者類(lèi)中所有的訂閱方法以及相關(guān)信息就結(jié)束了旦委。

6奇徒、SubscriberMethodFinder#getMethodsAndRelease()

private List<SubscriberMethod> getMethodsAndRelease(FindState findState) {
    List<SubscriberMethod> subscriberMethods = new ArrayList<>(findState.subscriberMethods);
    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;
}

這個(gè)方法的主要作用是對(duì)findState做初始化處理,釋放內(nèi)存缨硝,將findState保存到FindState池之前被置空的下標(biāo)中摩钙,方便下一次使用。然后返回SubscriberMethod對(duì)象列表查辩,保存到METHOD_CACHE中胖笛。

7、EventBus#subscribe()

public void register(Object subscriber) {
    Class<?> subscriberClass = subscriber.getClass();
    List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
    synchronized (this) {
        //  循環(huán)訂閱方法列表
        for (SubscriberMethod subscriberMethod : subscriberMethods) {
            //  訂閱
            subscribe(subscriber, subscriberMethod);
        }
    }
}

// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
    //  首先獲取訂閱方法的事件類(lèi)型
    Class<?> eventType = subscriberMethod.eventType;
    //  新建一個(gè)Subscription對(duì)象
    Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
    CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
    //  1. 如果 subscriptionsByEventType 中沒(méi)有以該事件類(lèi)型作為key值的存在宜岛,則添加长踊。
    if (subscriptions == null) {
        subscriptions = new CopyOnWriteArrayList<>();
        subscriptionsByEventType.put(eventType, subscriptions);
    } else {
        //  避免訂閱同一事件
        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++) {
        //  按照優(yōu)先級(jí)添加,等級(jí)越高在List中位置越靠前
        if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
            subscriptions.add(i, newSubscription);
            break;
        }
    }
        
    //  2. 將訂閱者類(lèi)中所有的事件類(lèi)型保存在map中萍倡,訂閱者類(lèi)的對(duì)象作為key值身弊,事件類(lèi)型列表作為value。
    List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
    if (subscribedEvents == null) {
        subscribedEvents = new ArrayList<>();
        typesBySubscriber.put(subscriber, subscribedEvents);
    }
    subscribedEvents.add(eventType);
        
    //  3. 判斷是否是粘性事件
    if (subscriberMethod.sticky) {
        //  默認(rèn)為true
        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);
        }
    }
}

我們把焦點(diǎn)再次聚焦到register()方法中,當(dāng)EventBus找到訂閱者類(lèi)中所有的訂閱方法時(shí)阱佛,會(huì)調(diào)用subscribe()方法帖汞。在subscribe()方法中主要有三個(gè)部分,分別對(duì)應(yīng)三個(gè)if判斷凑术,在上面代碼中都有一一講解翩蘸。

EventBus的事件訂閱到此就分析結(jié)束了,接下來(lái)要講的是事件的發(fā)布麦萤,它是怎么發(fā)布事件的鹿鳖?又是怎么切換線(xiàn)程的?

8壮莹、EventBus#post()

public void post(Object event) {
    //  獲取PostingThreadState對(duì)象
    PostingThreadState postingState = currentPostingThreadState.get();
    List<Object> eventQueue = postingState.eventQueue;
    eventQueue.add(event);
    //  判斷當(dāng)前事件是否正在發(fā)布狀態(tài)翅帜,避免連續(xù)重復(fù)操作。
    if (!postingState.isPosting) {
        //  是否是主線(xiàn)程
        postingState.isMainThread = isMainThread();
        postingState.isPosting = true;
        if (postingState.canceled) {
            throw new EventBusException("Internal error. Abort state was not reset");
        }
        try {
            while (!eventQueue.isEmpty()) {
                postSingleEvent(eventQueue.remove(0), postingState);
            }
        } finally {
            //  發(fā)布完成后回到初始狀態(tài)
            postingState.isPosting = false;
            postingState.isMainThread = false;
        }
    }
}

首先獲取一個(gè)PostingThreadState對(duì)象命满,里面包含了事件隊(duì)列eventQueue涝滴,然后把當(dāng)前事件添加到事件隊(duì)列中,如果當(dāng)前事件沒(méi)有處在發(fā)布狀態(tài)胶台,在則會(huì)進(jìn)入到if判斷中歼疮,最終會(huì)調(diào)用postSingleEvent()方法,并且通過(guò)eventQueue.remove(0)刪除已經(jīng)發(fā)布的事件诈唬。

9韩脏、EventBus#postSingleEvent()

private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
    //  獲取事件類(lèi)的Class類(lèi)型
    Class<?> eventClass = event.getClass();
    boolean subscriptionFound = false;
    //  默認(rèn)為true
    if (eventInheritance) {
        List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
        int countTypes = eventTypes.size();
        //  遍歷所有事件,包括這個(gè)事件類(lèi)本身铸磅、父類(lèi)以及實(shí)現(xiàn)的接口
        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));
        }
    }
}
9.1赡矢、EventBus#lookupAllEventTypes()
private static List<Class<?>> lookupAllEventTypes(Class<?> eventClass) {
    synchronized (eventTypesCache) {
        //  獲取和這個(gè)事件類(lèi)相關(guān)的class列表(包括父類(lèi)和接口)
        List<Class<?>> eventTypes = eventTypesCache.get(eventClass);
        if (eventTypes == null) {
            eventTypes = new ArrayList<>();
            Class<?> clazz = eventClass;
            //  循環(huán)添加這個(gè)事件類(lèi)的父類(lèi)和接口,并作為事件保存和發(fā)送
            while (clazz != null) {
                eventTypes.add(clazz);
                addInterfaces(eventTypes, clazz.getInterfaces());
                clazz = clazz.getSuperclass();
            }
            eventTypesCache.put(eventClass, eventTypes);
        }
        return eventTypes;
    }
}

這個(gè)方法主要是獲取Event事件類(lèi)及其父類(lèi)和接口的所有class列表阅仔,并以key/value的形式添加到eventTypesCache這個(gè)緩存對(duì)象性中吹散,方便下次直接使用,提升性能八酒。

10空民、EventBus#postSingleEventForEventType()

private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
    CopyOnWriteArrayList<Subscription> subscriptions;
    synchronized (this) {
        //  根據(jù)事件類(lèi)型獲取對(duì)應(yīng)Subscription列表
        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 {
                //  回到初始狀態(tài)
                postingState.event = null;
                postingState.subscription = null;
                postingState.canceled = false;
            }
            if (aborted) {
                break;
            }
        }
        return true;
    }
    return false;
}

postSingleEventForEventType()方法主要是獲取訂閱了該事件類(lèi)型的所有的Subscription列表,然后遍歷列表對(duì)里面的每個(gè)對(duì)象調(diào)用postToSubscription()方法羞迷。

11界轩、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 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);
    }
}

到了這里EventBus的事件發(fā)布解析就結(jié)束了。在這個(gè)方法中首先獲取當(dāng)前事件的訂閱方法的線(xiàn)程模型闭树,然后根據(jù)線(xiàn)程模型選擇對(duì)應(yīng)的線(xiàn)程去完成事件的發(fā)布耸棒。invokeSubscriber()主要是利用反射去調(diào)用方法,這樣就實(shí)現(xiàn)了訂閱事件->發(fā)布事件->接收事件這一過(guò)程报辱。

12、EventBus#unregister()

public synchronized void unregister(Object subscriber) {
    //  獲取當(dāng)前訂閱者訂閱的所有事件
    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());
    }
}

這里主要是把訂閱者傳進(jìn)去,得到當(dāng)前訂閱者訂閱的所有的事件列表碍现,遍歷是列表移除與訂閱者相關(guān)的所有信息幅疼。

12.1、EventBus#unsubscribeByEventType()
private void unsubscribeByEventType(Object subscriber, Class<?> eventType) {
    //  獲取事件類(lèi)型對(duì)應(yīng)的Subscription集合
    List<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
    if (subscriptions != null) {
        int size = subscriptions.size();
        //  遍歷subscriptions列表昼接,移除所有信息
        for (int i = 0; i < size; i++) {
            Subscription subscription = subscriptions.get(i);
            if (subscription.subscriber == subscriber) {
                subscription.active = false;
                subscriptions.remove(i);
                i--;
                size--;
            }
        }
    }
}

總結(jié)

到此爽篷,EventBus的分析就全部結(jié)束了,核心部分集中在訂閱和發(fā)布慢睡。訂閱主要是為了找到所有的訂閱方法和事件并保存相互關(guān)聯(lián)的信息逐工,發(fā)布主要是從保存的相互關(guān)聯(lián)的信息當(dāng)中找到以被發(fā)布的事件類(lèi)型作為參數(shù)的所有訂閱方法并通過(guò)反射調(diào)用該方法。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末漂辐,一起剝皮案震驚了整個(gè)濱河市泪喊,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌髓涯,老刑警劉巖袒啼,帶你破解...
    沈念sama閱讀 211,194評(píng)論 6 490
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異纬纪,居然都是意外死亡蚓再,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 90,058評(píng)論 2 385
  • 文/潘曉璐 我一進(jìn)店門(mén)包各,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)摘仅,“玉大人,你說(shuō)我怎么就攤上這事问畅⊥奘簦” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 156,780評(píng)論 0 346
  • 文/不壞的土叔 我叫張陵按声,是天一觀(guān)的道長(zhǎng)膳犹。 經(jīng)常有香客問(wèn)我,道長(zhǎng)签则,這世上最難降的妖魔是什么须床? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 56,388評(píng)論 1 283
  • 正文 為了忘掉前任,我火速辦了婚禮渐裂,結(jié)果婚禮上豺旬,老公的妹妹穿的比我還像新娘。我一直安慰自己柒凉,他們只是感情好族阅,可當(dāng)我...
    茶點(diǎn)故事閱讀 65,430評(píng)論 5 384
  • 文/花漫 我一把揭開(kāi)白布。 她就那樣靜靜地躺著膝捞,像睡著了一般坦刀。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開(kāi)封第一講書(shū)人閱讀 49,764評(píng)論 1 290
  • 那天鲤遥,我揣著相機(jī)與錄音沐寺,去河邊找鬼。 笑死盖奈,一個(gè)胖子當(dāng)著我的面吹牛混坞,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播钢坦,決...
    沈念sama閱讀 38,907評(píng)論 3 406
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼究孕,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了爹凹?” 一聲冷哼從身側(cè)響起厨诸,我...
    開(kāi)封第一講書(shū)人閱讀 37,679評(píng)論 0 266
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎逛万,沒(méi)想到半個(gè)月后泳猬,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 44,122評(píng)論 1 303
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡宇植,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 36,459評(píng)論 2 325
  • 正文 我和宋清朗相戀三年得封,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片指郁。...
    茶點(diǎn)故事閱讀 38,605評(píng)論 1 340
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡忙上,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出闲坎,到底是詐尸還是另有隱情疫粥,我是刑警寧澤,帶...
    沈念sama閱讀 34,270評(píng)論 4 329
  • 正文 年R本政府宣布腰懂,位于F島的核電站梗逮,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏绣溜。R本人自食惡果不足惜慷彤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,867評(píng)論 3 312
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望怖喻。 院中可真熱鬧底哗,春花似錦、人聲如沸锚沸。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 30,734評(píng)論 0 21
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)哗蜈。三九已至前标,卻和暖如春坠韩,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背候生。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 31,961評(píng)論 1 265
  • 我被黑心中介騙來(lái)泰國(guó)打工同眯, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留绽昼,地道東北人唯鸭。 一個(gè)月前我還...
    沈念sama閱讀 46,297評(píng)論 2 360
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像硅确,于是被迫代替她去往敵國(guó)和親目溉。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 43,472評(píng)論 2 348

推薦閱讀更多精彩內(nèi)容