Android EventBus3.0 索引

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类型事件"));


http://greenrobot.org/eventbus/documentation/subscriber-index

EventBus3.0——索引的使用

【腾讯Bugly干货】老司机教你“飙”EventBus3

Android事件总线(一)EventBus3.0用法全解析

Android事件总线(二)EventBus3.0源码解析

Android事件总线(三)otto用法全解析

Android事件总线(四)源码解析otto

猜你喜欢

转载自blog.csdn.net/luoguopeng/article/details/80525578