[EventBus源码解析] EventBus.register 方法详述

前情概要

  在上一篇中,介绍了EventBus的基本使用方法,以及一部分进阶技巧。本篇及以后的几篇blog将会集中解析EventBus.java,看看作者是如何优雅地实现这个看似简单的事件分发/接收机制。

本篇概述

  剖析register的过程,let's get started!

方法签名

  完整的register方法签名如下,我们通常调用的register(this)其实最终调用到的register(this, false, 0),另外,使用registerSticky(this)进行调用,其实最终也是走到同一个方法中。

private synchronized void register(Object subscriber, boolean sticky, int priority) {
    List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriber.getClass());
    for (SubscriberMethod subscriberMethod : subscriberMethods) {
        subscribe(subscriber, subscriberMethod, sticky, priority);
    }
}

 

  • 声明为synchronized,是因为其中调用的subscribe方法需要在同步块中执行;
  • 这个register方法其实只做了一件事:将目标class的所有onEvent方法取出(通过反射),逐一对事件订阅(通过subscribe)

  subscribe翻译过来就是“订阅、订购”,是本次重点解析的内容,下面来揭开她神秘的面纱:

subscribe/订阅

  对于熟悉Design Patterns的人来说,第一眼看到EventBus,脑海中浮现的一定是观察者模式/Observer,这个模式的实现方式,无非就是——事件的分发者拥有一个订阅者的列表,每次事件发生时,分发者遍历并通知列表中的订阅者。订阅者可以自由地加入/退出订阅列表。

  register是一个订阅的过程,故在subscribe中所做的无非就是“将目标类的目标方法加入到订阅者列表”这件事。让我们结合代码来看。

private void subscribe(Object subscriber, SubscriberMethod subscriberMethod, boolean sticky, int priority) {
    Class<?> eventType = subscriberMethod.eventType;
    CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
    Subscription newSubscription = new Subscription(subscriber, subscriberMethod, priority);
    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);

    int size = subscriptions.size();
    for (int i = 0; i <= size; i++) {
        if (i == size || newSubscription.priority > subscriptions.get(i).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 (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);
        }
    }
}

 

  Event是分发者/订阅者解耦的关键,两者就是通过Event的class进行通信的。故先取出目标Event的订阅列表(subscriptionsByEventType),将新规则加入进去,如果重复register则会抛出Exception。加入的过程中,会根据优先级决定插入到List中的位置(int值越大,位置越靠前,优先级也就越高)。接下来还会将subscriber(也就是目标class)与新的eventType的对应关系加入到typesBySubscriber中,这个Map在后续的查找中会用到。

  对于sticky的的情况,若该eventBus支持事件继承(eventInheritance == true),则将eventType及其祖先类的所有Event都重新分发一遍,若不支持则只分发目标eventType。(此时分发的Event皆来自于stickyEvents

unregister/注销

  本篇最后分析一下注销的过程,代码很简单。

public synchronized void unregister(Object subscriber) {
    List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
    if (subscribedTypes != null) {
        for (Class<?> eventType : subscribedTypes) {
            unubscribeByEventType(subscriber, eventType);
        }
        typesBySubscriber.remove(subscriber);
    } else {
        Log.w(TAG, "Subscriber to unregister was not registered before: " + subscriber.getClass());
    }
}

 

  unregister时,首先将eventtype - subscriber的绑定关系逐一解除(处理subscriptionsByEventType),最后把subscriber - eventTypes解绑。

下期预告

  剖析post过程

 

转载于:https://www.cnblogs.com/maozhige/p/4722384.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值