问题描述
我对Android中的EventBus和RxJava之间的区别感到困惑.对于我的有关在完成某些更改后通知某些组件的问题,我需要实现其中之一,以便它们可以更新其状态.
另外,我读到EventBus已被RxJava弃用,我不知道此信息是否正确.
I am confused about the difference between EventBus and RxJava in android. I need to implement one of them for my issue about notifying some components when some changes have been done, so that they can update their state.
Also, I read that EventsBus has became deprecated over RxJava and I don't know if this information is true or not.
推荐答案
EventBus
和 RxJava
的性质不同.
EventBus
只是一个 bus
-它提供了将事件订阅和发布到"bus"的机制,而无需关心接线方式,总线"实际上是什么,等等.在Android的上下文中, EventBus
是处理发送和接收 Broadcast
消息更少样板的一种简便方法.
EventBus
is just a bus
as the name suggest - it provides the mechanism to subscribe and publish events to the "bus", without you caring how the wiring is done, what this "bus" actually is, etc. In the context of Android, the EventBus
is just an easier way to deal with sending and receiving Broadcast
messages with less boilerplate.
RxJava
的功能要强大得多.是的,您可以订阅和发布事件,但是您可以对进程进行更多控制-频率,所有线程在哪个线程上发生等等. RxJava
(我认为)的主要功能是您可以使用大量的 operators
来操纵发布的数据非常容易.
RxJava
on the other hand is much much more powerful than that. Yes, you can subscribe and publish events, but you have far more control over the process - frequency, on which thread everything happens, etc. The main power of RxJava
(in my opinion) is that you can manipulate the data being published very easy, using some of its tons of operators
.
总而言之-如果您只关心发布一些事件并在收到消息时执行某些操作-最好使用两者中最简单的一种,即某种 Bus
,或者甚至是普通的 BroadcastReceiver
.如果您也将从转换数据,处理线程或简化错误处理方面受益,请使用 RxJava
方法.请记住, RxJava
通常具有陡峭的学习曲线,因此要花一些时间来适应其概念.
To sum up - if you only care about publishing some events and performing some actions when received - you'd probably be better off using the simplest of the two, namely some kind of Bus
, or even plain old BroadcastReceiver
s. If you will also benefit of transforming the data, handling threading or simplified error handling - go for the RxJava
approach. Just keep in mind that RxJava
generally has a steep learning curve, so it takes some time to get used to its concept.
这篇关于EventBus和RxJava有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!