Android专业人士缺点:事件总线和RxJava [英] Android Pros & Cons: Event Bus and RxJava

查看:187
本文介绍了Android专业人士缺点:事件总线和RxJava的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在我的应用程序(即greenrobot/EventBus)中使用事件总线.但是我发现使用事件总线有一些缺点:

I have been using Event Bus in my apps (i.e: greenrobot/EventBus). But I find some disadvantages in using Event Bus:

  • 束缚任务执行很困难
  • 很多代表事件的类
  • 代码较少(嗯,仍然可以跟踪,但不是很清楚)

我一直在研究用于解决此问题的新技术.而且我读了很多有关RxJava的文章,想知道它是否可以解决.

I have been researching for new techniques to deal with this problem. And I read quite a bit about RxJava and wonder if it could be a solution.

所以我对RxJava的问题(基于我最近阅读的内容):

So my questions about RxJava (based on what I have read recently):

  • 是否可以随时注册RxJava观察者?因此,不仅是在创建Observable时.有了EventBus,我可以随时进行订阅,而不仅是在创建Observable时.
  • 您如何处理两个或多个发布相同类型事件(例如导航事件)的发布商?
  • 紧密耦合发布者和订阅者,这意味着我每次必须明确指定发布者.因此,我不仅要担心事件的类型,还要担心发起者.使用EventBus,我只需要担心事件的类型,而不必担心发起者.

推荐答案

1)一旦有了Observable的实例,就可以随时从任何线程(甚至同时)订阅它.

1) Once you have an instance of an Observable, you can subscribe to it at any time and from any thread, even concurrently.

2)我们通常通过Observable.merge()合并多个可观察对象的流,或使用序列化的PublishSubject.

2) We usually merge the streams of multiple observables via Observable.merge() or use a serialized PublishSubject.

3)如果您观​​察到一个Observable,则可能涉及数十个上游运算符和源,但是无论如何,您都会获得顺序的值流.您只需要握住代表事件某些来源的Observable,观察者就无需知道事件是否已合并,过滤,通过网络往返并在到达onNext()方法之前被延迟.您自然可以实现或使用一些查找服务来使您获得一个Observable来减少耦合,但是对于RxJava,耦合通常不是问题.

3) If you observe an Observable, there could be dozens of upstream operators and sources involved, but you'll get a sequential stream of values no matter what. You only need to get a hold onto an Observable representing some source of events and the observer doesn't need to know if the event was merged, filtered, made a roundtrip over the network and got delayed before arriving in your onNext() method. You can naturally implement or use some lookup service that will get you an Observable to reduce the coupling, but with RxJava, coupling is not usually an issue.

这篇关于Android专业人士缺点:事件总线和RxJava的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆