美文网首页
EventBus源码阅读总结

EventBus源码阅读总结

作者: allsunny | 来源:发表于2018-08-07 11:30 被阅读0次

EventBus主要关注两个方法就行了。一个是register(),一个是post()。


register():
2.x版本,用反射的方式扫描了所有的方法,把匹配的方法(以onEvent开头,只有一个参数的方法)最终保存在
subscriptionsByEventType(Map,key:eventType ; value:CopyOnWriteArrayList<Subscription> )中;
3x版本使用了注解,方法名不用以onEvent开头。扫描方法的方式从反射变成了apt的方式。

eventType是我们方法参数的Class(Event类),
Subscription中则保存着subscriber, subscriberMethod(method, threadMode, eventType),
priority;包含了执行改方法所需的一切。


post():
遍历eventTypes(Event类以及它的父类),到subscriptionsByEventType去查找subscriptions;
遍历每个subscription,反射调用相关方法。

Method类的invoke()方法:对带有指定参数的指定对象,调用此Method对象表示的底层方法
public native Object invoke(Object obj, Object... args)
example:subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
方法.invoke(register所在的类(Activity/Fragment),post传入的参数event)

参考:
https://blog.csdn.net/lmj623565791/article/details/40920453
http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2016/0417/4152.html
http://www.10tiao.com/html/227/201607/2650236358/1.html
https://juejin.im/post/5a3e19c26fb9a0452207b6b5
https://www.jianshu.com/p/f8fd67eef9aa

相关文章

网友评论

      本文标题:EventBus源码阅读总结

      本文链接:https://www.haomeiwen.com/subject/oqpovftx.html