本文首發(fā):http://yuweiguocn.github.io/
本文介紹了EventBus 3.0的新特性蛔垢,新增訂閱者索引代替反射以提升性能及新加@Subscribe注解等。
《竹枝詞》
楊柳青青江水平,聞郎江上唱歌聲。
東邊日出西邊雨,道是無晴卻有晴。
-唐僵控,劉禹錫
首先,添加依賴:
compile 'org.greenrobot:eventbus:3.0.0'
訂閱事件
從EventBus 3開始鱼冀,我們需要在處理訂閱事件的方法上添加@Subscribe注解报破,方法名可以任意取悠就。我們可以使用threadMode指定分發(fā)的線程。
ThreadMode有四種充易,和以前的版本差不多:
//和post為同一線程梗脾,這也是默認的
@Subscribe(threadMode = ThreadMode.POSTING)
public void onMessage(MessageEvent event) {
log(event.message);
}
//回調(diào)在Android主線程
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessage(MessageEvent event) {
textField.setText(event.message);
}
//回調(diào)在后臺線程
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onMessage(MessageEvent event){
saveToDisk(event.message);
}
//回調(diào)在單獨的線程
@Subscribe(threadMode = ThreadMode.ASYNC)
public void onMessage(MessageEvent event){
backend.send(event.message);
}
粘性事件
發(fā)送Sticky和之前一樣:
EventBus.getDefault().postSticky(new MessageEvent("Hello everyone!"));
指定sticky參數(shù)為true,接收Sticky事件:
@Override
public void onStart() {
super.onStart();
EventBus.getDefault().register(this);
}
// UI updates must run on MainThread
@Subscribe(sticky = true, threadMode = ThreadMode.MAIN)
public void onEvent(MessageEvent event) {
textField.setText(event.message);
}
@Override
public void onStop() {
EventBus.getDefault().unregister(this);
super.onStop();
}
獲取粘性事件:
MessageEvent stickyEvent = EventBus.getDefault().getStickyEvent(MessageEvent.class);
// Better check that an event was actually posted before
if(stickyEvent != null) {
// "Consume" the sticky event
EventBus.getDefault().removeStickyEvent(stickyEvent);
// Now do something with it
}
移除粘性事件:
MessageEvent stickyEvent = EventBus.getDefault().removeStickyEvent(MessageEvent.class);
// Better check that an event was actually posted before
if(stickyEvent != null) {
// Now do something with it
}
優(yōu)先級和取消事件
訂閱優(yōu)先級盹靴,默認為0:
@Subscribe(priority = 1);
public void onEvent(MessageEvent event) {
...
}
注意:不同線程模式之間的訂閱者的優(yōu)先級不受分發(fā)順序的影響炸茧。
取消事件分發(fā):
// Called in the same thread (default)
@Subscribe
public void onEvent(MessageEvent event){
// Process the event
...
// Prevent delivery to other subscribers
EventBus.getDefault().cancelEventDelivery(event) ;
}
事件通常可以被高優(yōu)先級訂閱者取消稿静。取消事件分發(fā)的事件處理方法只能運行在ThreadMode.POSTING線程梭冠。
訂閱索引(Subscriber Index)
訂閱索引是EventBus 3的新特性,它是一個可選的優(yōu)化用于加快訂閱者注冊的初始化自赔。
我們可以通過EventBus提供的注解處理器在編譯的時候創(chuàng)建訂閱索引妈嘹。使用索引不是必須的,但為了在Android上有更好的性能推薦使用绍妨。
注意:只有添加了@Subscriber注解的訂閱者才可以被索引并且事件Event類是public的。由于Java注解處理器本身的限制柬脸,匿名內(nèi)部類中的@Subscribe注解不會被識別他去。
當(dāng)EventBus不能使用索引的時候,它會在運行時自動回退到反射模式倒堕。因此灾测,它還會正常工作,只是有點小慢垦巴。
有兩種方法可以生成索引:
1.使用annotationProcessor
如果你使用的不是Android Gradle Plugin 2.2.0 或更高版本媳搪,請使用第二種方法。
我們需要添加一個annotationProcessor屬性設(shè)置eventBusIndex參數(shù)用于指定你想生成索引的全類名骤宣。
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'
}
2.使用android-apt
如果上面的不能使用秦爆,你可以使用Gradle Plugin android-apt 添加EventBus注解處理器。
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"
}
}
當(dāng)你下次編譯的時候憔披,通過eventBusIndex
指定的類會被生成等限。然后我們可以像這樣指定索引類:
EventBus eventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();
或者如果你想使用默認的實例:
EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
// Now the default instance uses the given index. Use it like this:
EventBus eventBus = EventBus.getDefault();
你可以使用同樣的方法應(yīng)用到類庫中,這樣就有可能會有多個索引類芬膝,可以通過下面的方法指定多個索引類:
EventBus eventBus = EventBus.builder()
.addIndex(new MyEventBusAppIndex())
.addIndex(new MyEventBusLibIndex()).build();
混淆
-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);
}