wzgiceman / rxbus Goto Github PK
View Code? Open in Web Editor NEW实现和EventBus3.0用法一样的RxBus
Home Page: http://blog.csdn.net/wzgiceman/article/details/52701265
实现和EventBus3.0用法一样的RxBus
Home Page: http://blog.csdn.net/wzgiceman/article/details/52701265
无论用CURRENT_THREAD,MAIN,NEW_THREAD,IO都打印显示main线程
例如我在A,B两个页面都register一个string类型的event,在C页面post一个string消息后,会invoke四次.
第一次:subscriber A触发,在rxbus中的callEvent这个中会得到List,A,B都在该list中,所以遍历的时候回执行2次invoke,
第二次subscriber B同理触发了2次,总计为4次.
在我看来subscriber A只观察A中注册的event,B只观察B中注册的event,而不应该是A接收消息时会附带执行B中同类型的event.这样A,B只关心自己的event,A+B总体上来看同一event还是被遍历一次而不影响以前的逻辑,多以在callEvent过滤掉部分事件即可.
/**
* 回调到订阅者的方法中
*
* @param code code
* @param object obj为event的类型
* @param subscriber 注册消息时的订阅者对象
*/
private void callEvent(int code, Object object, Object subscriber) {
Class eventClass = object.getClass();
//获取event类型的所有方法
List<SubscriberMethod> methods = subscriberMethodByEventType.get(eventClass);
if (methods != null && methods.size() > 0) {
for (SubscriberMethod subscriberMethod : methods) {
//订阅者和注册消息时的订阅者对象为同一对象时才执行该方法体
if (subscriberMethod.subscriber == subscriber) {
//......原来for中的逻辑
}
}
}
}
调用的地方就为:callEvent(subscriberMethod.code, o,subscriberMethod.subscriber);
这样就可以多次被触发
sticky事件第一、二次接收正常,第三次后会重复多次收到
第三次收到4次,第四次收到9次,第五次收到15次
接收
@subscribe(threadMode = ThreadMode.MAIN, sticky = true)
public void onEvent(Events.MyEvent event){
Log.d(TAG,"onEvent");
}
发送
RxBus.getDefault().post(new Events.MyEvent());
在Service注册Rxbus后,在一个页面发送消息Service能收到消息,退出页面后重新进入,再次发送,Service无法收到消息
case R.id.btn_sticky: RxBus.getDefault().post(new EventStickText("我是sticky消息")); Intent intent = new Intent(this, Main2Activity.class); startActivity(intent); break;
一加手机,7.0系统 没有效果
在post事件时报错, 看了下应该是
// 主题
private final Subject bus;
这里没有泛型导致类型检查错误,然后没有onError处理导致异常
加上泛型后正常了
private final Subject<Object, Object> bus;
能否封装个异步加载数据的
上传到jcenter,页面上没有Add to Jcenter按钮是咋回事?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.