EventBus之Subscriber Index

Subscriber Index

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

Subscriber Index在编译时(build time)使用EventBus注解处理器(annotation processor)创建。虽然不是必须要使用索引,但建议在Android上使用索引以获得最佳性能。

Index Preconditions

注意,只能为订阅者和事件类为公共的(subscriber AND event class are public)@Subscriber方法建立索引。此外,由于Java注解处理(annotation process)本身的技术限制,@Subscribe注解不能在匿名类中识别(not recognized inside of anonymous classes)。

当EventBus不能使用索引时,它将在运行时(run time)自动回退到(fallback to)反射(reflection)。因此它仍然可以工作,只是稍微慢一点。

EventBusBuilder有一些默认属性,有一个属性为ignoreGeneratedIndex,这个属性为true时会使用反射方法获取订阅者的事件处理函数,为false时会使用subscriber Index生成的SubscriberInfo来获取订阅者的事件处理函数,首先,Subscriber Index会在编译期间生成SubscriberInfo,然后在运行时使用SubscriberInfo中保存的事件处理函数处理事件,减少了反射时需要的耗时,会有运行速度上的提升。

如何生成索引(How to generate the index)

使用annotationProcessor

如果您不使用Android Gradle插件版本2.2.0或更高版本,请使用 Android-apt 配置。

要启用索引生成,您需要使用 annotationProcessor 属性将EventBus注释处理器添加到构建中。还可以设置参数eventBusIndex来指定要生成的索引的完全限定类。例如,添加以下部分到您的Gradle构建脚本:


android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [ eventBusIndex : 'com.example.myapp.MyEventBusIndex' ]
            }
        }
    }
}
 
dependencies {
    implementation 'org.greenrobot:eventbus:3.1.1'
    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}

使用kapt

如果你想在Kotlin代码中使用EventBus,你需要使用kapt而不是annotationProcessor:
 

apply plugin: 'kotlin-kapt' // ensure kapt plugin is applied

dependencies {
    implementation 'org.greenrobot:eventbus:3.1.1'
    kapt 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}

kapt {
    arguments {
        arg('eventBusIndex', 'com.example.myapp.MyEventBusIndex')
    }
}

使用android-apt

如果上面的方法不起作用,您可以使用 android-apt Gradle插件将EventBus注释处理器添加到构建中。添加以下部分到您的Gradle构建脚本:

buildscript {
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    compile 'org.greenrobot:eventbus:3.1.1'
    apt 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}

apt {
    arguments {
        eventBusIndex "com.example.myapp.MyEventBusIndex"
    }
}

如何使用索引

成功构建项目之后,将为您生成eventBusIndex指定的类。然后当设置EventBus时,像这样传递它:

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();

 MyEventBusIndex的路径为:项目根目录\build\generated\source\apt\debug\包名\MyEventBusIndex.java

Indexing your Libraries

您可以将相同的原理应用于作为类库的一部分的代码(而不仅是主的应用程序(and not the final application))。这样,你可能有多个索引类,你可以在EventBus设置(setup)过程中添加,例如:

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

GitHub地址

由于作者水平有限,语言描述及代码实现中难免有纰漏,望各位看官多提宝贵意见!

Hello , World !

感谢所有!

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

windfallsheng

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值