翻译:通过 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 了!

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

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

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

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

  4. Pingback: skydiving long island

  5. Pingback: where can i get garcinia cambogia extract

  6. Pingback: double glazing windows

  7. Pingback: pickleball paddle

  8. Pingback: hotels burlington

  9. Pingback: commercial water slide

  10. Pingback: rocky mount

  11. Pingback: interserver coupon

  12. Pingback: interserver promo code

  13. Pingback: Click Here

  14. Pingback: click here

  15. Pingback: read more

  16. Pingback: useful reference

  17. Pingback: my company

  18. Pingback: a fantastic read

  19. Pingback: original site

  20. Pingback: why not try these out

  21. Pingback: check these guys out

  22. Pingback: you can try this out

  23. Pingback: navigate to these guys

  24. Pingback: hop over to this website

  25. Pingback: More Bonuses

  26. Pingback: Continued

  27. Pingback: pop over to this website

  28. Pingback: see post

  29. Pingback: look here

  30. Pingback: read more

  31. Pingback: best info

  32. Pingback: news

  33. Pingback: how to

  34. Pingback: about

  35. Pingback: imp source

  36. Pingback: a fantastic read

  37. Pingback: my response

  38. Pingback: i thought about this

  39. Pingback: check these guys out

  40. Pingback: view publisher site

  41. Pingback: why not find out more

  42. Pingback: their explanation

  43. Pingback: Visit Website

  44. Pingback: her latest blog

  45. Pingback: More Bonuses

  46. Pingback: check that

  47. Pingback: you could check here

  48. Pingback: you could try here

  49. Pingback: Learn More

  50. Pingback: Get the facts

  51. Pingback: official statement

  52. Pingback: important source

  53. Pingback: click here

  54. Pingback: read more

  55. Pingback: click here

  56. Pingback: click here

  57. Pingback: read more

  58. Pingback: read more

  59. Pingback: read more

  60. Pingback: read more

  61. Pingback: website

  62. Pingback: click here

  63. Pingback: read more

  64. Pingback: more news

  65. Pingback: check this link right here now

  66. Pingback: official site

  67. Pingback: check these guys out

  68. Pingback: her latest blog

  69. Pingback: Clicking Here

  70. Pingback: More hints

  71. Pingback: best value

  72. Pingback: new source

  73. Pingback: best news

  74. Pingback: learn now

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

  76. Pingback: click here

  77. Pingback: read more

  78. Pingback: important

  79. Pingback: find

  80. Pingback: figure out

  81. Pingback: continue reading

  82. Pingback: what to do

  83. Pingback: where to look

  84. Pingback: insight

  85. Pingback: quality article

  86. Pingback: Sperry boots for women

  87. Pingback: More about the author

  88. Pingback: useful reference

  89. Pingback: i thought about this

  90. Pingback: their explanation

  91. Pingback: Homepage

  92. Pingback: a fantastic read

  93. Pingback: find this

  94. Pingback: browse this site

  95. Pingback: my response

  96. Pingback: why not try these out

  97. Pingback: More hints

  98. Pingback: Get the facts

  99. Pingback: you could look here

  100. Pingback: official statement

  101. Pingback: try this web-site

  102. Pingback: explanation

  103. Pingback: discover this info here

  104. Pingback: mitogen vs superantigen

  105. Pingback: sperrys for cheap

  106. Pingback: read more

  107. Pingback: More about the author

  108. Pingback: click site

  109. Pingback: my review here

  110. Pingback: get redirected here

  111. Pingback: useful reference

  112. Pingback: Get More Info

  113. Pingback: how to get bigger boobs

  114. Pingback: how to get bigger boobs naturally

  115. Pingback: this website

  116. Pingback: great post to read

  117. Pingback: my company

  118. Pingback: imp source

  119. Pingback: click to read more

  120. Pingback: see it here

  121. Pingback: how to get bigger boobs

  122. Pingback: Homepage

  123. Pingback: a fantastic read

  124. Pingback: find this

  125. Pingback: Bonuses

  126. Pingback: read this article

  127. Pingback: click here now

  128. Pingback: check here

  129. Pingback: original site

  130. Pingback: my response

  131. Pingback: this page

  132. Pingback: navigate to this website

  133. Pingback: news

  134. Pingback: have a peek at these guys

  135. Pingback: have a peek at these guys

  136. Pingback: click here

  137. Pingback: click here

  138. Pingback: click here

  139. Pingback: click here

  140. Pingback: read more

  141. Pingback: read more

  142. Pingback: read more

  143. Pingback: what is new

  144. Pingback: what is new

  145. Pingback: what is new

  146. Pingback: what is new

  147. 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.

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