EventBus3.0新特性之Subscriber Index

最近在读EventBus3.0源码,发现了一个问题,EventBus.getDefault().register(this) 这种方式其实还是使用反射机制获取注解属性和方法的,并没有使用到注解处理器。

一 什么是Subscriber Index

Subscriber Index(订阅者索引)是一个可选择的优化技术,用来加速初始化subscriber注册。
通过使用EventBus annotation processor(EventBus注解处理器),订阅者索引在编译期间就会被创建。虽然没有规定必须使用它,但是由于它在Android中最佳性能,官方推荐使用此方式。

二 使用索引的先决条件

注意只有用@Subscriber注解的方法才能被编入索引到,同时subscriber类和事件类必须是public。并且,由于Java注解处理本身的技术限制,@Subscribe 注解不能使用在匿名类中。
当EventBus不能使用索引,它将自动恢复到在运行时通过反射的方式,因此它也能正常工作,只是变得更慢了。

三 生成索引的两种方法

方法一:使用注解处理器

如果你不是使用 Android Gradle Plugin version 2.2.0 或更高版本,请以android-apt方式来配置。
启用索引生成功能,你需要使用annotationProcessor 属性把EventBus注解处理器添加到你的build。另外,设置一个eventBusIndex参数去指定你想要产生索引类的全路径类名。比如说,添加下面的部分到你的Gradle build 脚本。

android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [ eventBusIndex : 'com.example.myapp.MyEventBusIndex' ]
            }
        }
    }
}
 
dependencies {
    compile 'org.greenrobot:eventbus:3.0.0'
    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.0.1'
}
方法二:使用android-apt

打开App的build.gradle,在dependencies中添加最新的EventBus依赖:

compile 'org.greenrobot:eventbus:3.0.0'

在项目gradle的dependencies中引入apt编译插件:

classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'

然后在App的build.gradle中应用apt插件,并设置apt生成的索引的包名和类名:

apply plugin: 'com.neenbedankt.android-apt'
apt {
    arguments {
        eventBusIndex "com.study.sangerzhong.studyapp.MyEventBusIndex"
    }
}

接着在App的dependencies中引入EventBusAnnotationProcessor:

apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'

在Rebuild Project操作前,确保有@Subscribe注解的方法,否则不会生成索引类。 Rebuild Project后,就会发现在\ProjectName\app\build\generated\source\apt\PakageName\ 下看到通过注解处理器生成的索引类,这样我们便可以在初始化EventBus时应用我们生成的索引了。

在这里会生成类名为MyEventBusIndex的索引类:

/** This class is generated by EventBus, do not edit. */
public class MyEventBusIndex implements SubscriberInfoIndex {
    private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX;

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

        putIndex(new SimpleSubscriberInfo(MainActivity.class, true, new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("onEventA", PayEvent.class, ThreadMode.ASYNC),
        }));

        putIndex(new SimpleSubscriberInfo(MyIntentService.class, true, new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("onEventBB", PayEvent.class),
        }));

    }

    private static void putIndex(SubscriberInfo info) {
        SUBSCRIBER_INDEX.put(info.getSubscriberClass(), info);
    }

    @Override
    public SubscriberInfo getSubscriberInfo(Class<?> subscriberClass) {
        SubscriberInfo info = SUBSCRIBER_INDEX.get(subscriberClass);
        if (info != null) {
            return info;
        } else {
            return null;
        }
    }
}

可以看出是使用一个静态HashMap来保存订阅类的相关信息,其中包括了订阅类的class对象,是否需要检查父类,以及所有事件响应函数的信息。

三 怎么把索引类应用到Android

当你实例化EventBus时,通过以下方式:

EventBus eventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();

或者,你想要在整个app中使用设置了索引类的EventBus实例作为默认单例。

EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
// Now the default instance uses the given index. Use it like this:
EventBus eventBus = EventBus.getDefault();

在3.0版本中,EventBus提供了一个EventBusAnnotationProcessor注解处理器来在编译期通过读取@Subscribe注解,并解析和处理其中所包含的信息,然后生成java类来保存订阅者中所有的事件响应函数,这样就比在运行时使用反射来获得订阅者中所有事件响应函数的速度要快。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
EventBus是一种用于Android应用程序中发布/订阅事件的库。它遵循发布-订阅模式,允许不同组件之间进行松散耦合的通信。 在基于EventBus 3.0的APP开发中,你可以按照以下步骤进行: 1. 添加EventBus依赖 在项目的build.gradle文件中添加以下代码: ``` dependencies { implementation 'org.greenrobot:eventbus:3.2.0' } ``` 2. 创建事件类 创建一个事件类,它将包含你需要发送和接收的数据。例如: ``` public class MessageEvent { public final String message; public MessageEvent(String message) { this.message = message; } } ``` 3. 注册订阅者 在需要接收事件的组件中,注册订阅者。例如,在Activity中: ``` @Override public void onStart() { super.onStart(); EventBus.getDefault().register(this); } @Override public void onStop() { EventBus.getDefault().unregister(this); super.onStop(); } ``` 4. 发布事件 在需要发送事件的组件中,发布事件。例如,在Activity中: ``` EventBus.getDefault().post(new MessageEvent("Hello, world!")); ``` 5. 处理事件 在订阅者中,创建一个方法来处理接收到的事件。例如,在Activity中: ``` @Subscribe(threadMode = ThreadMode.MAIN) public void onMessageEvent(MessageEvent event) { // Do something with the event Toast.makeText(this, event.message, Toast.LENGTH_SHORT).show(); } ``` 以上就是基于EventBus 3.0的APP开发的基本步骤。通过使用EventBus,你可以轻松地在不同组件之间传递数据,从而实现应用程序中的松散耦合通信。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值