gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:50:56 27 4
gpt4 key购买 nike

我们的 Android 应用目前使用 Otto EventBus,它使用反射。我们希望避免反射的开销,但要保持灵 active 。是否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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com