EventBus性能优化-添加索引(index)
如何添加索引?
1.Android Gradle Plugin version 2.2.0或以上,如:classpath 'com.android.tools.build:gradle:2.2.0',使用annotationProcessor
模块的build.gradle中
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.0'
}
2.Android Gradle Plugin version 2.2.0以下,使用android-apt
项目的build.gradle中
buildscript {
dependencies {
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
}
}
模块的build.gradle中
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
compile 'org.greenrobot:eventbus:3.0.0'
apt 'org.greenrobot:eventbus-annotation-processor:3.0.0'
}
apt {
arguments {
eventBusIndex "com.example.myapp.MyEventBusIndex"
}
}
此时编译一下,就会在module\build\generated\source\apt\PakageName\下看到生成的索引类
如何使用索引?
//不使用索引
EventBus.getDefault().register(this);
EventBus.getDefault().post(new MessageEvent("MessageEvent类型事件"));
//使用索引
//传入索引对象new MyEventBusIndex(),并传给默认单例EventBus对象
EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
EventBus.getDefault().register(this);
EventBus.getDefault().post(new MessageEvent("MessageEvent类型事件"));
---------------------
作者:guopeng_233
来源:优快云
原文:https://blog.youkuaiyun.com/luoguopeng/article/details/80525578
版权声明:本文为博主原创文章,转载请附上博文链接!