EventBus学习笔记

该文为个人学习笔记,如有错误,烦请大佬指出~~~

说明:
1.onCreate 注册: EventBus.getDefault().register(this);
--注册后,进行遍历标注为EventBus处理的方法(onMessageEvent、),并进行保存
--标注处理方法:(3.0之前及之后的不同标注方式)
方法名:onEventMainThread、
注解:@Subscribe(threadMode = ThreadMode.MAIN)
2.onDestroy 注销: EventBus.getDefault().unregister(this);
3.调用:EventBus.getDefault().post(param);

1.Register
    --调用注册后、反射遍历寻找标注为EventBus处理的方法,并且会找寻其父类中的标注方法
    --subscriptionsByEventType 存储匹配到的方法
        参数:key:subscriber ,value:List
        eventType 是方法参数的class
        Subscription 保存subscriber、subscriberMethod
        priority 包含了执行方法的一切
        
2.post
    --currentPostingThreadState是一个ThreadLocal类型的,里面存储了PostingThreadState;
    --PostingThreadState包含了一个eventQueue和一些标志位。
    
    
    --把我们传入的event,保存到了当前线程中的一个变量PostingThreadState的eventQueue中。
    --判断当前是否是UI线程。
    --遍历队列中的所有的event,调用postSingleEvent(eventQueue.remove(0), postingState)方法
    --遍历所有的Class,到subscriptionsByEventType去查找subscriptions
    --遍历每个subscription,依次去调用postToSubscription(subscription, event, postingState.isMainThread);

3.postSticky
    --发送事件之后再订阅该事件也能收到该事件,跟黏性广播类似
    --黏性事件处理函数->@Subscribe(sticky = true)

总结:register会把当前类中匹配的方法,存入一个map,而post会根据实参去map查找进行反射调用

学习博客地址:http://blog.csdn.net/lmj623565791/article/details/40920453

你可能感兴趣的:(EventBus学习笔记)