- Notifications
You must be signed in to change notification settings - Fork41
🚌 The RxBus as steady as an old dog.
License
NotificationsYou must be signed in to change notification settings
Blankj/RxBus
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Gradle:
implementation"com.blankj:rxbus:1.6"
- 注册事件
publicclassYourActivityextendsActivity {@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 注册 String 类型事件RxBus.getDefault().subscribe(this,newRxBus.Callback<String>() {@OverridepublicvoidonEvent(Strings) {Log.e("eventTag",s); } });// 注册带 tag 为 "my tag" 的 String 类型事件RxBus.getDefault().subscribe(this,"my tag",newRxBus.Callback<String>() {@OverridepublicvoidonEvent(Strings) {Log.e("eventTag",s); } }); }@OverrideprotectedvoidonDestroy() {super.onDestroy();// 注销RxBus.getDefault().unregister(this); }}
- 发送事件
// 发送 String 类型事件RxBus.getDefault().post("without tag");// 发送带 tag 为 "my tag" 的 String 类型事件RxBus.getDefault().post("with tag","my tag");
- 发送事件
// 发送 String 类型的粘性事件RxBus.getDefault().postSticky("without tag");// 发送带 tag 为 "my tag" 的 String 类型的粘性事件RxBus.getDefault().postSticky("with tag","my tag");// 在需要移除粘性事件的地方移除它RxBus.getDefault().removeSticky("without tag");RxBus.getDefault().removeSticky("with tag","my tag");
- 注册事件
publicclassYourActivityextendsActivity {@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);// 注册 String 类型事件RxBus.getDefault().subscribeSticky(this,newRxBus.Callback<String>() {@OverridepublicvoidonEvent(Strings) {Log.e("eventTag",s); } });// 注册带 tag 为 "my tag" 的 String 类型事件RxBus.getDefault().subscribeSticky(this,"my tag",newRxBus.Callback<String>() {@OverridepublicvoidonEvent(Strings) {Log.e("eventTag",s); } }); }@OverrideprotectedvoidonDestroy() {super.onDestroy();// 注销RxBus.getDefault().unregister(this); }}
如果用到事件总线的地方比较多,那么可以把事件总线的使用放到一个 Manager 中使用,比如我 Demo 中做的封装如下所示:
publicclassRxBusManager {privatestaticfinalStringMY_TAG ="MY_TAG";publicstaticvoidsubscribeRxBusManagerActivity(finalRxBusManagerActivityactivity){RxBus.getDefault().subscribe(activity,newRxBus.Callback<String>() {@OverridepublicvoidonEvent(Strings) {activity.updateText("without " +s); } });RxBus.getDefault().subscribe(activity,MY_TAG,newRxBus.Callback<String>() {@OverridepublicvoidonEvent(Strings) {activity.updateText("with " +s); } }); }publicstaticvoidpostToRxBusManagerActivity(finalStringevent) {RxBus.getDefault().post(event); }publicstaticvoidpostWithMyTagToRxBusManagerActivity(finalStringevent) {RxBus.getDefault().post(event,MY_TAG); }publicstaticvoidpostStickyToRxBusManagerActivity(finalStringevent) {RxBus.getDefault().postSticky(event); }publicstaticvoidpostStickyWithMyTagToRxBusManagerActivity(finalStringevent) {RxBus.getDefault().postSticky(event,MY_TAG); }publicstaticvoidunregisterRxBusManagerActivity(finalRxBusManagerActivityactivity) {RxBus.getDefault().unregister(activity); }}
可以看出这是在 RxBusManagerActivity 中要使用 RxBus 的相关代码,这样可以更方便地管理应用中所有的事件总线,而不至于发了个事件都不清楚接收方在哪的尴尬。
网上现有 RxBus 存有的问题:
- 使用的 RxBus 大多停留在 RxJava1 版本
- RxBus 实现的粘性事件很多都是有问题的
- 如果事件抛了异常,之后便再也无法接收到的问题
- 同类型事件需自己再次封装 Bean 进行区别。
介于以上问题,我还是亲自封装一个简洁的供大家使用,库已经依赖了 RxAndroid 和 RxJava,所以导入了该库的就不需要再额外导入那两库了。
当然,如果通信频率比较高追求效率的话还是推荐使用EventBus。
- 利用 FlowableProcessor 既可以作为观察者又可以作为被观察者来实现事件总线
- 粘性事件原理就是发送的时候把事件存到一个 hash 表中,在注册的时候查询下 hash 表中是否存在符合的事件,有的话就消费掉即可
- 替换原有 LambdaSubscriber 来让抛了异常之后可以继续接收到后续事件
- 封装了 TagMessage 来区分不同类别的 tag
- 动态识别范型对象来省去传入 Type 类型
还有一些细节就自己看源码去了解吧,总共有用的代码不超过 300 行哈。
欢迎加入我的小专栏「基你太美」一起学习。
About
🚌 The RxBus as steady as an old dog.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.