EventBus 3.0 新特性

本文首发:http://yuweiguocn.github.io/

本文介绍了EventBus 3.0的新特性,新增订阅者索引代替反射以提升性能及新加@Subscribe注解等。

《竹枝词》
杨柳青青江水平,闻郎江上唱歌声。
东边日出西边雨,道是无晴却有晴。
-唐,刘禹锡

首先,添加依赖:

compile 'org.greenrobot:eventbus:3.0.0'

订阅事件

从EventBus 3开始,我们需要在处理订阅事件的方法上添加@Subscribe注解,方法名可以任意取。我们可以使用threadMode指定分发的线程。

ThreadMode有四种,和以前的版本差不多:

//和post为同一线程,这也是默认的
@Subscribe(threadMode = ThreadMode.POSTING)
public void onMessage(MessageEvent event) {
    log(event.message);
}

//回调在Android主线程
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessage(MessageEvent event) {
  textField.setText(event.message);
}

//回调在后台线程
@Subscribe(threadMode = ThreadMode.BACKGROUND)
public void onMessage(MessageEvent event){
    saveToDisk(event.message);
}

//回调在单独的线程
@Subscribe(threadMode = ThreadMode.ASYNC)
public void onMessage(MessageEvent event){
    backend.send(event.message);
}

粘性事件

发送Sticky和之前一样:

EventBus.getDefault().postSticky(new MessageEvent("Hello everyone!"));

指定sticky参数为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
}

优先级和取消事件

订阅优先级,默认为0:

@Subscribe(priority = 1);
public void onEvent(MessageEvent event) {
    ...
}

注意:不同线程模式之间的订阅者的优先级不受分发顺序的影响。

取消事件分发:

// Called in the same thread (default)
@Subscribe
public void onEvent(MessageEvent event){
    // Process the event
    ...
    // Prevent delivery to other subscribers
    EventBus.getDefault().cancelEventDelivery(event) ;
}

事件通常可以被高优先级订阅者取消。取消事件分发的事件处理方法只能运行在ThreadMode.POSTING线程。

订阅索引(Subscriber Index)

订阅索引是EventBus 3的新特性,它是一个可选的优化用于加快订阅者注册的初始化。

我们可以通过EventBus提供的注解处理器在编译的时候创建订阅索引。使用索引不是必须的,但为了在Android上有更好的性能推荐使用。

注意:只有添加了@Subscriber注解的订阅者才可以被索引并且事件Event类是public的。由于Java注解处理器本身的限制,匿名内部类中的@Subscribe注解不会被识别。

当EventBus不能使用索引的时候,它会在运行时自动回退到反射模式。因此,它还会正常工作,只是有点小慢。

有两种方法可以生成索引:

1.使用annotationProcessor
如果你使用的不是Android Gradle Plugin 2.2.0 或更高版本,请使用第二种方法。
我们需要添加一个annotationProcessor属性设置eventBusIndex参数用于指定你想生成索引的全类名。

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"
    }
}

当你下次编译的时候,通过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();

你可以使用同样的方法应用到类库中,这样就有可能会有多个索引类,可以通过下面的方法指定多个索引类:

EventBus eventBus = EventBus.builder()
    .addIndex(new MyEventBusAppIndex())
    .addIndex(new MyEventBusLibIndex()).build();

混淆

-keepattributes *Annotation*
-keepclassmembers class ** {
    @org.greenrobot.eventbus.Subscribe ;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }

# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    (java.lang.Throwable);
}

参考

  • http://greenrobot.org/eventbus/documentation

你可能感兴趣的:(EventBus 3.0 新特性)