java - GreenRobot 和 Guava 的 EventBus 使用反射吗?

标签 java reflection event-bus otto greenrobot-eventbus

我们的 Android 应用程序当前使用 Otto EventBus,它使用反射。我们希望避免反射的开销,但保持灵活性。 是Guava's event bus使用反射?怎么样GreenRobot的?

如果他们不这样做,他们会使用代码生成或类似的东西吗?

最佳答案

Otto 从来没有像 GreenRobot 的 EventBus 那样功能丰富 - 例如,没有线程模式,所以最好摆脱它。 Otto 被弃用,转而支持 RxJava - 这对于许多项目来说是巨大的杀伤力(个人观点)。



但是为了减少反射的使用,GreenRobot EventBus 3.x is able to build an index in compilation time using APT而不是运行时反射。

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


Index Preconditions: Note that only @Subscriber methods can be indexed for which the subscriber AND event class are public. Also, due to technical limitations of Java’s annotation processing itself, @Subscribe annotations are not recognized inside of anonymous classes.

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

And

EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
// Now the default instance uses the given index. Use it like this:
EventBus eventBus = EventBus.getDefault();

关于java - GreenRobot 和 Guava 的 EventBus 使用反射吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40743304/

相关文章:

java - 确定应用程序是通过 Java 还是 Javaw 启动的?

java - 根据调用方法更改返回类型

reflection - Go:我怎样才能 "unpack"一个结构?

android - greenrobot事件总线的ProGuard配置

java - EventBus的onEvent方法如何使用?

java - HTTPClient StringEntity PUT 问题

java - 多个实例与单个静态实例

java - 每次滚动/平移/拖动时,相机都会移回到原始位置

ios - Swift:如何获取具有可用数组 MirrorType 的数组元素类型

dom - 为什么我们在 DOM 中需要 event.stopPropagation() ?这是不好的架构模式吗?