背景
以前用的 otto 来持有 eventbus,今天一看他们官网居然不玩了。。。于是根据官方指导引入了 rxAndroid 来实现 bus。跟着这份手册写了个简单的 bus
Bus 实现
https://gist.github.com/jiachenning/34236178ddd5d0d1154a21b38c909429
Bus 用法
_rxBus.send(new UserLogin());
_rxBus.toObserverable()
.subscribe(new Consumer<Object>() {
@Override
public void accept(@NonNull Object obj) throws Exception {
if(obj instanceof UserNotLoginException){
Toaster.showShort(activity, "Userlogin");
}else {
Toaster.showShort(activity, "unknown type");
}
}
});
一些疑问
- 有一个 event 被 send 出来后,所有相关的 consumer 都会被 invoke,然后用 instance of 关键词来筛选。能不能改成 send(topic, event)的这种方式,这样在 invoke consumer 的前能筛选。
- 这种绑定会有泄露问题吗?
- 还有没有欠考虑的地方?(第一天用 rxJava 根本没感觉啊