EventBus3官方文档翻译(三)
Subscriber Index
订阅者索引(index)是EventBus3的新特性,它是可选择的最佳化来加速初始化订阅者注册;订阅者索引在build时用EventBus 注解处理器创造,然而不是强制要求使用index,只是推荐使用,可以获得更好的性能;
- Index Preconditions(索引的前置条件)
只有订阅者和事件类是public时,才可以在@Subscriber方法上添加index,由于java注解处理本身技术的限制,@Subscribe 不可以用于匿名类;当EventBus不能使用index,它会自动地在运行时回退到反射机制,因此,它仍然有效,只是慢一点;
How to generate the index(如何生成index)
- Using annotationProcessor
如果你不在使用Android Gradle Plugin 版本2.2.0 or higher,使用android-apt来配置;
为了生成索引,你需要在你的build文件添加EventBus注解处理器:annotationProcessor,设置一个argument eventBusIndex来指定你想要index 类,例如,添加下面的部分到你的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'
- Using android-apt
如果上面不起作用,你可以添加EventBus 注解处理器到你的build文件,使用android-apt Gradle插件,添加下面的内容到你的Gradle build 文件;
buildscript {
dependencies {
classpath 'com.neenbedankt.gradle.plugins(插件):android-apt:1.8'
}
}
apply plugin: 'com.neenbedankt.android-apt'
dependencies {
compile 'org.greenrobot:eventbus:3.0.0'
apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'
}
apt {
arguments {
eventBusIndex "com.example.myapp.MyEventBusIndex"
}
}
当你build project时,如果没有错误产生,则会生成指定的eventBusIndex,之后就可以这样设置EventBus:
EventBus eventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();
或者,如果你想在app任意处使用默认的实例:
EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
// Now the default instance uses the given index. Use it like this:
EventBus eventBus = EventBus.getDefault();
Indexing your Libraries
你可以使用相同的原理来code你的部分library,这样的话,你就会有多个index classes,你可以在设置EventBus时,把它们全部加进来,例如:
EventBus eventBus = EventBus.builder()
.addIndex(new MyEventBusAppIndex())
.addIndex(new MyEventBusLibIndex()).build();
ProGuard(混淆)
在你的proguard配置文件中使用下面的规则,避免订阅者被删除;
-keepattributes *Annotation*
-keepclassmembers class ** {
@org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
<init>(java.lang.Throwable);
}
提示:不管你有没有使用subscriber index,你都需要这样配置
AsyncExecutor
AsyncExecutor就像一个线程池(thread pool),但是具有failture(exception)处理,failture时会抛出异常,AsyncExecutor会自动的包裹这些异常,并且post出去
声明:AsyncExecutor 不是核心使用类,他可能在background线程中处理错误时减少你的代码,但它不是EventBus的核心类;
通常地,你使用AsyncExecutor.create()创建一个实例,把它保存在Application范围内,然后实现RunnableEx接口,传递到AsyncExecutor方法中execute,不同于Runnable,RunnableEx可能会抛出异常。
如果RunnableEx接口实现中抛出了异常,异常会被捕获和包裹在 ThrowableFailureEvent中,会post出去;
execution例如:
AsyncExecutor.create().execute(
new AsyncExecutor.RunnableEx() {
@Override
public void run() throws LoginException {
// No need to catch any Exception (here: LoginException)
remote.login();
EventBus.getDefault().postSticky(new LoggedInEvent());
}
}
);
receiving part例如:
@Subscribe(threadMode = ThreadMode.MAIN)
public void handleLoginEvent(LoggedInEvent event) {
// do something
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void handleFailureEvent(ThrowableFailureEvent event) {
// do something
}
AsyncExecutor Builder
如果你想定制你的AsyncExecutor实例,你可以回调AsyncExecutor.builder()这个方法,它会返回一个Builder让你来做定制化,线程池,failture事件;