记得上次写EventBus还是在上次(一年前,哈哈),转眼间又是一年了,发现对于EventBus的源码细节有点模糊,挖个坑捋捋EventBus的源码
由于项目中使用且当前最新版本源码变化不大,本文贴出的源码基于EventBus3.0.0,关于EventBus的用法可以移步我之前的文章:
Android EventBus你需要了解的都在这
EventBus.register
首先,EventBus的原理是基于发布订阅模式,有些萌新开始会以为是观察者模式,其实它跟观察者模式的区别在于不需要观察者(订阅者)和目标(发布者)直接交互,而是通过调度中心来进行分发事件,实现了它们之间的解耦。
看源码首先可以从我们使用的第一步,注册订阅事件的方法register 开始:
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
其中
SubscriberMethodFinder 是EventBus用来管理订阅事件的方法类SubscriberMethod 是EventBus的订阅方法(使用@Subscribe注解的方法),其中封装了订阅者的回调方法、线程模式、EventClass、是否为粘性事件等信息
EventBus通过传入的Object对象subscriber 的class,构造出了一个SubscriberMethod 数组,看看构造数组的findSubscriberMethods 方法中有什么
SubscriberMethodFinder.findSubscriberMethods
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) {
return subscriberMethods;
}
if (ignoreGeneratedIndex) {
subscriberMethods = findUsingReflection(subscriberClass);
} else {
subscriberMethods = findUsingInfo(subscriberClass);
}
if (subscriberMethods.isEmpty()) {
throw new EventBusException("Subscriber " + subscriberClass
+ " and its super classes have no public methods with the @Subscribe annotation");
} else {
METHOD_CACHE.put(subscriberClass, subscriberMethods);
return subscriberMethods;
}
}
简单说,findSubscriberMethods 方法中主要是通过反射获取目标类中添加了@Subscriber注解的方法数组
然后register() 中会遍历方法数组,调用subscribe 方法订阅:
EventBus.subscribe
其中
Subscription 是包裹订阅类和订阅者信息的包装类SubscriberMethod 是上边说过的EventBus的订阅方法,其中封装了订阅者的回调方法、线程模式、EventClass、是否为粘性事件等信息,为了防止各位弄混,再强调一下typesBySubscriber 是EventBus中存放的“订阅者-事件类型”列表
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
Class<?> eventType = subscriberMethod.eventType;
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions == null) {
subscriptions = new CopyOnWriteArrayList<>();
subscriptionsByEventType.put(eventType, subscriptions);
} else {
if (subscriptions.contains(newSubscription)) {
throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
+ eventType);
}
}
int size = subscriptions.size();
for (int i = 0; i <= size; i++) {
if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
subscriptions.add(i, newSubscription);
break;
}
}
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
subscribedEvents.add(eventType);
if (subscriberMethod.sticky) {
if (eventInheritance) {
Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
for (Map.Entry<Class<?>, Object> entry : entries) {
Class<?> candidateEventType = entry.getKey();
if (eventType.isAssignableFrom(candidateEventType)) {
Object stickyEvent = entry.getValue();
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
} else {
Object stickyEvent = stickyEvents.get(eventType);
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
}
对于普通事件,添加到subscribedEvents 后该时间就处理完毕了,低于黏性事件,则还会使用checkPostStickyEventToSubscription(newSubscription, stickyEvent); 方法继续向该观察者通知所有的黏性事件
private void checkPostStickyEventToSubscription(Subscription newSubscription, Object stickyEvent) {
if (stickyEvent != null) {
postToSubscription(newSubscription, stickyEvent, Looper.getMainLooper() == Looper.myLooper());
}
}
private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
switch (subscription.subscriberMethod.threadMode) {
case POSTING:
invokeSubscriber(subscription, event);
break;
case MAIN:
if (isMainThread) {
invokeSubscriber(subscription, event);
} else {
mainThreadPoster.enqueue(subscription, event);
}
break;
case BACKGROUND:
if (isMainThread) {
backgroundPoster.enqueue(subscription, event);
} else {
invokeSubscriber(subscription, event);
}
break;
case ASYNC:
asyncPoster.enqueue(subscription, event);
break;
default:
throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
}
}
这里就是post处理事件的逻辑了, 到这里本篇EventBus.register 注册方法的解析就结束了,详细的代码解析会放到后续的处理post事件解析
|