Android技术知识Android开发

EventBus源码解析(四):优化-EventBusIndex

2020-08-21  本文已影响0人  涅小黑

EventBus源码解析(一):概述
EventBus源码解析(二):Register
EventBus源码解析(三):Post/PostSticky
EventBus源码解析(四):优化-EventBusIndex

如果使用EventBusIndex就不用通过反射来查找订阅者的回调方法了,可以很大地优化性能。

        SUBSCRIBER_INDEX = new HashMap<Class<?>, SubscriberInfo>();

        putIndex(new SimpleSubscriberInfo(SecondActivity.class, true, new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("onMessageEvent", Object.class, ThreadMode.MAIN, 0, true),
        }));

        putIndex(new SimpleSubscriberInfo(MainActivity.class, true, new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("onMessageEvent", Object.class, ThreadMode.MAIN),
        }));

APT主要生成上面的代码,可见是把每一个订阅者的方法进行了拆解,在应用初始化的时候调用

EventBus.builder().addIndex(MyEventBusIndex()).installDefaultEventBus()

就可以把所有的监听者分解到前面说的容器里面

上一篇 下一篇

猜你喜欢

热点阅读