Android进阶之路Android开发经验谈Android技术知识

面试官问我:Android EventBus的源码,看我如何用漫

2020-05-21  本文已影响0人  像程序一样思考

一名优秀的Android开发,需要一份完备的 知识体系,在这里,让我们一起成长为自己所想的那样~。

基本使用步骤

1)定义事件Event

public class MyEvent {
    public MyEvent(String id, String message) {
        this.id = id;
        this.message = message;
    }

    public String id;

    public String message;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }
}

2)注册订阅者,并实现回调方法

public class MainActivity extends Activity {

    private TextView message, next;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initView();
        initData();
    }

    /**
     * 初始化布局组件
     */
    private void initView() {
        message = findViewById(R.id.message);
        next = findViewById(R.id.next);

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                startActivity(intent);
            }
        });
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMyEvent(MyEvent event) {
        message.setText(event.getMessage());
    }

    /**
     * 初始化数据
     */
    private void initData() {
        EventBus.getDefault().register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}

通过EventBus.getDefault().register(this);方法注册订阅者。
3)发送消息

public class SecondActivity extends Activity {

    private TextView message;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        initView();
    }

    /**
     * 初始化布局组件
     */
    private void initView() {
        message = findViewById(R.id.message);
        message.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EventBus.getDefault().post(new MyEvent("1","这是从SecondActivity发送过来的消息"));
                Toast.makeText(SecondActivity.this, "消息发送成功", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

通过EventBus.getDefault().post(new MyEvent("1","这是从SecondActivity发送过来的消息"));方法发送消息

源码分析

EventBus的使用先注册订阅者,并实现方法,然后再发送post消息,所以我们分析源码也按照这个顺序来。
1)实例化EventBus

EventBus.getDefault().register(this);

该方法首先获取EventBus实例,然后再注册,源码如下所示:

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

可以看到,实例化EventBus是个双重锁的单例模式。

接下来看EventBus的构造函数,源码如下:

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

可以看到,在EventBus的构造函数中,初始化了一大堆变量,这边主要关注前面两个。

subscriptionsByEventType = new HashMap<>();:key:事件类型(如:MyEvent ),value:新的订阅对象的集合,包括订阅者和订阅者包含的方法。具体赋值逻辑在后面会讲,这边先了解即可。
typesBySubscriber = new HashMap<>();:key:事件的订阅者(MainActivity ),value:事件类型的集合。订阅者跟事件类型是一对多的关系,所以一个界面可以支持多个事件类型。

2)注册

 /**
     * Registers the given subscriber to receive events. Subscribers must call {@link #unregister(Object)} once they
     * are no longer interested in receiving events.
     * <p/>
     * Subscribers have event handling methods that must be annotated by {@link Subscribe}.
     * The {@link Subscribe} annotation also allows configuration like {@link
     * ThreadMode} and priority.
     */
    public void register(Object subscriber) {
        //1.拿到订阅对象的类型;
        Class<?> subscriberClass = subscriber.getClass();
        //2.通过findSubscriberMethods方法获取该订阅者中的所有订阅方法,因为可能包含多个订阅方法,所以返回集合。
        List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
        synchronized (this) {
            for (SubscriberMethod subscriberMethod : subscriberMethods) {
               //3.通过subscribe方法为每个订阅方法进行订阅。 
                subscribe(subscriber, subscriberMethod);
            }
        }
    }

如何获取该订阅者中的所有订阅方法,这边主要看第二件事情的处理过程,及findSubscriberMethods方法的源码

List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
        //先拿订阅对象在本地缓存中查找,提高性能。
        List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
        if (subscriberMethods != null) {
            return subscriberMethods;
        }
        //默认情况下ignoreGeneratedIndex值是false的
        if (ignoreGeneratedIndex) {
           //使用反射方法拿到订阅者中的订阅方法  
            subscriberMethods = findUsingReflection(subscriberClass);
        } else {
            //使用编译期间生成的SubscriberInfo
            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;
        }
    }

可以看到上面方法做了三件事:
1.先拿订阅对象在本地缓存中查找订阅方法
2.如果本地获取不到,则根据ignoreGeneratedIndex的值决定获取订阅方法的方式
3.本地保存订阅方法。
可以看到获取订阅的关键方法为:findUsingInfo(),那么赶紧看下他的源码,如下:

private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
        FindState findState = prepareFindState();
        findState.initForSubscriber(subscriberClass);
        while (findState.clazz != null) {
            // 获取订阅者信息,没有配置MyEventBusIndex返回null
            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 {
                // 通过反射来查找订阅方法,所以为了提高性能,我们还是要用索引的形式使用EventBus
                findUsingReflectionInSingleClass(findState);
            }
            findState.moveToSuperclass();
        }
        return getMethodsAndRelease(findState);
    }

获取到订阅方法后,我们看真正的注册方法subscribe(subscriber, subscriberMethod);

    // Must be called in synchronized block
    private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
        //获取到事件类型(如:MyEvent )
        Class<?> eventType = subscriberMethod.eventType;
        //把订阅者和订阅者方法重新封装成新的对象
        Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
        //事件类型为key,新的订阅者对象列表为value,存储进subscriptionsByEventType
        //该集合很重要,当post消息的时候,就是从该集合中查找订阅者对象列表
        CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
        if (subscriptions == null) {
            //如果订阅者对象列表为空,则初始化出来,并加到subscriptionsByEventType中
            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++) {
            // 根据订阅方法的优先级,添加到订阅列表
            if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
                subscriptions.add(i, newSubscription);
                break;
            }
        }
        //订阅者为key,事件类型列表为value,存储进typesBySubscriber
        List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
        if (subscribedEvents == null) {
            subscribedEvents = new ArrayList<>();
            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);
            }
        }
    }

可以看到上面方法做了两件事:
1.根据订阅方法的优先级,添加到订阅列表,事件类型为key,订阅列表为value,存储进subscriptionsByEventType
2.订阅者为key,事件类型列表为value,存储进typesBySubscriber

3)发送消息

    /** Posts the given event to the event bus. */
    public void post(Object event) {
        PostingThreadState postingState = currentPostingThreadState.get();
        List<Object> eventQueue = postingState.eventQueue;
        //将当前要发送的事件加入到队列中
        eventQueue.add(event);

        if (!postingState.isPosting) {
            postingState.isMainThread = isMainThread();
            postingState.isPosting = true;
            if (postingState.canceled) {
                throw new EventBusException("Internal error. Abort state was not reset");
            }
            try {
                 //循环从列表中拿event
                while (!eventQueue.isEmpty()) {
                    postSingleEvent(eventQueue.remove(0), postingState);
                }
            } finally {
                postingState.isPosting = false;
                postingState.isMainThread = false;
            }
        }
    }

循环从eventQueue取event,调用postSingleEvent方法:

private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
        Class<?> eventClass = event.getClass();
        boolean subscriptionFound = false;
        if (eventInheritance) {
            //查找event事件和event子类事件
            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));
            }
        }
    }

遍历每个event事件或者子类,会再调用postSingleEventForEventType:

private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
        CopyOnWriteArrayList<Subscription> subscriptions;
        synchronized (this) {
            //subscriptionsByEventType有没有很眼熟,在subscribe方法中,事件类型为key,新的订阅者对象列表为value,存储进subscriptionsByEventType
            //获取到新的订阅对象列表
            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;
    }

可以看到,该方法其实就是从subscriptionsByEventType集合中,查找该event对应的订阅者对象列表,然后遍历订阅者对象,调用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);
        }
    }

最后通过反射调用订阅者的方法。
通过上面的分析可以看到发送消息其实是三重循环:
1.循环从eventQueue取event,调用postSingleEvent方法
2.遍历每个event事件或者子类,调用postSingleEventForEventType方法
3.遍历订阅者对象,调用postToSubscription方法。
这种方式的算法复杂度还是很高的,event事件不做继承可以提高发送流程的性能。

4)解绑

 /** Unregisters the given subscriber from all event classes. */
    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());
        }
    }

可以看到,先从typesBySubscriber集合中根据订阅者获取到事件类型列表subscribedTypes,然后循环事件类型,找出该事件类型对应的Subscriptions订阅列表,循环遍历,当Subscription对象中包含该订阅者subscriber对象时,把该Subscription对象从订阅列表中删除,再从从typesBySubscriber中删除对应的订阅者。

上一篇下一篇

猜你喜欢

热点阅读