翻译:通过 RxJava 实现一个 Event Bus – RxBus

原文:Implementing an Event Bus With RxJava – RxBus

这篇文章有三个部分:

  1. 关于什么是 Event Bus(事件总线)的快速指引(不译)
  2. 通过 RxJava 实现一个 RxBus
  3. 对于文章所题的这种方式的一些疑惑和回答

一、什么是 Event Bus(事件总线)

这部分由于篇幅太长,并且不是关键,所以略去,关于这部分,我假设你已经懂得了,如果不懂,可以看看 Square 的 Otto 或者 GreenRobot 的 EventBus,再或者你可以看看 妹纸&gank.io 的源代码,这个项目使用到了 Otto 和 RxJava。

二、通过 RxJava 实现一个 RxBus

我已经发布了这个 使用 RxJava 的例子了: 一个使用 RxJava for Android 的教程 ,所以我在这里继续来解释 RxBus 的完整实现。这是其中有趣部分的实现:

// 这是作为中间人调用的对象
public class RxBus {

  private final Subject<Object, Object> _bus = new SerializedSubject<>(PublishSubject.create());

  public void send(Object o) {
    _bus.onNext(o);
  }

  public Observable<Object> toObserverable() {
    return _bus;
  }
}

这样就可以了,看得出我们已经有一个 Event Bus 可以使用啦,接下来就是使用方式的介绍:

通过这个 bus 发布一个事件:

@OnClick(R.id.btn_demo_rxbus_tap)
public void onTapButtonClicked() {

    _rxBus.send(new TapEvent());
}

在其他 Fragment 或者 Service 等地方监听这个事件:

// 注意 要使用原本发布这个事件的那个 bus 实例才行
_rxBus.toObserverable()
    .subscribe(new Action1<Object>() {
      @Override
      public void call(Object event) {

        if(event instanceof TapEvent) {
          _showTapText();

        }else if(event instanceof SomeOtherEvent) {
          _doSomethingElse();
        }
      }
    });

在这个例子中,我们在绿色部分的 Frgament 中发布了一个点击事件,然后在蓝色部分的 Fragment 中监听响应这个事件。

三、对文章所题的这种方式的一些疑惑和回答

关于 Dead events,以及 RxBus 要怎么实现这点?

我们如果能够知道是否有观察者目前订阅了我们的 bus,在有些情况下是很有用的。例如,你使用一个 Event Bus 来处理 GCM 的推送通知,当你的 App 当前正在前台运行的时候,你不想发送通知栏通知,这时监听这个 “DeadEvent” 就很重要了。(译者注:来源于 Otto 中对于 DeadEvent 的用处解释:用来包装一个没有订阅者的事件,它不会被交付。)

例如,在我们最新发布的一个 App 中,我们增加了一个“发送中”的功能。当用户打开我们的 App 在使用,这时会有许多监听器正在监听这个 bus,我们不需要发送 通知栏消息,当我们的 App 在后台的时候,我们需要发送 通知栏消息 让用户获得一些聊天信息。当一个事件发布,如果没有订阅者在监听(注:如果应用或页面到了后台或者关闭,一些和生命周期挂钩的订阅,都会解除对事件的监听,所以这时可以说没有订阅者在监听啦),就发布一个 dead event。如果我们发出一个 dead event,推送通知栏消息就可以出现了。

那么 RxBus 要怎么实现这点?

事实上十分简单,在 1.x 版本的 RxJava 中有了一个很有用的方法:hasObservers(),使用这个方法就可以做到啦。

那么我是否应该使用 RxBus 来替代 Otto/EventBus?

如果你只是想简单地使用一个 Event Bus,你最好还是使用 Otto(特别推荐)或者 EventBus。Otto 通过注解可以有很清爽的 API,并且它使用起来也比较简单。

而如果你对 Rx 很熟悉,并且已经在你的项目中使用了 RxJava,同时你又想避免使用更多的依赖库,那么肯定说要试试我们的 RxBus 了!

  1. 请问楼主,用 RxBus 怎么订阅 List 这种类型的数据呢?另外,RxBus 需要及时取消订阅么?

  2. Otto:
    This project is deprecated in favor of RxJava and RxAndroid. These projects permit the same event-driven programming model as Otto, but they’re more capable and offer better control of threading.

    If you’re looking for guidance on migrating from Otto to Rx, this post is a good start.

  3. Pingback: what is new

  4. Pingback: what is new

  5. Pingback: what is new

  6. Pingback: what is new

  7. Pingback: read more

  8. Pingback: read more

  9. Pingback: read more

  10. Pingback: click here

  11. Pingback: click here

  12. Pingback: click here

  13. Pingback: click here

  14. Pingback: have a peek at these guys

  15. Pingback: have a peek at these guys

  16. Pingback: news

  17. Pingback: navigate to this website

  18. Pingback: this page

  19. Pingback: my response

  20. Pingback: original site

  21. Pingback: check here

  22. Pingback: click here now

  23. Pingback: read this article

  24. Pingback: Bonuses

  25. Pingback: find this

  26. Pingback: a fantastic read

  27. Pingback: Homepage

  28. Pingback: how to get bigger boobs

  29. Pingback: see it here

  30. Pingback: click to read more

  31. Pingback: imp source

  32. Pingback: my company

  33. Pingback: great post to read

  34. Pingback: this website

  35. Pingback: how to get bigger boobs naturally

  36. Pingback: how to get bigger boobs

  37. Pingback: Get More Info

  38. Pingback: useful reference

  39. Pingback: get redirected here

  40. Pingback: my review here

  41. Pingback: click site

  42. Pingback: More about the author

  43. Pingback: read more

  44. Pingback: sperrys for cheap

  45. Pingback: mitogen vs superantigen

  46. Pingback: discover this info here

  47. Pingback: explanation

  48. Pingback: try this web-site

  49. Pingback: official statement

  50. Pingback: you could look here

  51. Pingback: Get the facts

  52. Pingback: More hints

  53. Pingback: why not try these out

  54. Pingback: my response

  55. Pingback: browse this site

  56. Pingback: find this

  57. Pingback: a fantastic read

  58. Pingback: Homepage

  59. Pingback: their explanation

  60. Pingback: i thought about this

  61. Pingback: useful reference

  62. Pingback: More about the author

  63. Pingback: Sperry boots for women

  64. Pingback: quality article

  65. Pingback: insight

  66. Pingback: where to look

  67. Pingback: what to do

  68. Pingback: continue reading

  69. Pingback: figure out

  70. Pingback: find

  71. Pingback: important

  72. Pingback: read more

  73. Pingback: click here

  74. Pingback: Free makeup samples 2016 - Give Her Makeup

  75. Pingback: learn now

  76. Pingback: best news

  77. Pingback: new source

  78. Pingback: best value

  79. Pingback: More hints

  80. Pingback: Clicking Here

  81. Pingback: her latest blog

  82. Pingback: check these guys out

  83. Pingback: official site

  84. Pingback: check this link right here now

  85. Pingback: more news

  86. Pingback: read more

  87. Pingback: click here

  88. Pingback: website

  89. Pingback: read more

  90. Pingback: read more

  91. Pingback: read more

  92. Pingback: read more

  93. Pingback: click here

  94. Pingback: click here

  95. Pingback: read more

  96. Pingback: click here

  97. Pingback: important source

  98. Pingback: official statement

  99. Pingback: Get the facts

  100. Pingback: Learn More

  101. Pingback: you could try here

  102. Pingback: you could check here

  103. Pingback: check that

  104. Pingback: More Bonuses

  105. Pingback: her latest blog

  106. Pingback: Visit Website

  107. Pingback: their explanation

  108. Pingback: why not find out more

  109. Pingback: view publisher site

  110. Pingback: check these guys out

  111. Pingback: i thought about this

  112. Pingback: my response

  113. Pingback: a fantastic read

  114. Pingback: imp source

  115. Pingback: about

  116. Pingback: how to

  117. Pingback: news

  118. Pingback: best info

  119. Pingback: read more

  120. Pingback: look here

  121. Pingback: see post

  122. Pingback: pop over to this website

  123. Pingback: Continued

  124. Pingback: More Bonuses

  125. Pingback: hop over to this website

  126. Pingback: navigate to these guys

  127. Pingback: you can try this out

  128. Pingback: check these guys out

  129. Pingback: why not try these out

  130. Pingback: original site

  131. Pingback: a fantastic read

  132. Pingback: my company

  133. Pingback: useful reference

  134. Pingback: read more

  135. Pingback: click here

  136. Pingback: Click Here

  137. Pingback: interserver promo code

  138. Pingback: interserver coupon

  139. Pingback: rocky mount

  140. Pingback: commercial water slide

  141. Pingback: hotels burlington

  142. Pingback: pickleball paddle

  143. Pingback: double glazing windows

  144. Pingback: where can i get garcinia cambogia extract

  145. Pingback: skydiving long island

  146. 妹纸&gank.io 的作者居然就是你,膜拜一下,我最近在参考你的代码写个类似的demo

  147. 博主,是否觉得 如果使用RxJava的话, 可以不需要 Otto了呢

  148. 看起来,好像要区分事件,只能在事件接收的地方来判断,而不是在事件发起的地方判断了?

    • 作者的风格,或许是 iOS 编程习惯带过来的。我翻译就保留了,但其实我也不喜欢这种下划线写法