EventBus高效使用及源码解析

引言

EventBus是square发布的一个用于事件订阅和发布的框架,其最大的贡献在于将事件的订阅和发布很好地解耦,使代码更优雅,逻辑更清晰。
EventBus的主要特点如下:

  • 组件解耦
    • 解耦事件订阅和发布者
    • 在Activitives,Fragments和后台线程的使用中表现良好
    • 避免了复杂且易导致错误的依赖和生命周期问题
  • 简化代码
  • 足够快
  • 轻量(大约50K)
  • 已经在100,000,000+个应用上得到了证明
  • 具有一些高级特色,如负责传递的线程,订阅优先级,粘性事件等.

下面就让我们一起揭开EventBus的神秘面纱

1.EventBusAnnotationProcessor:EventBus的正确打开方式

网上有很多介绍EventBus的文章,但是几乎没有提到EventBusAnnotationProcessor的。实际上,从EventBus 3开始引入了注解,它的主要作用在于使用注解而非反射来解析订阅信息,并且这个过程是在编译时而非运行时完成的,因而可使EventBus中的事件订阅节约很多时间。

要使用EventBus 3的这个新特性,需要以下几步:

  • 添加依赖
1
compile 'org.greenrobot:eventbus:3.0.0'
  • 由于注解依赖android-apt-plugin,故需要在项目的gradle的dependencies中引入apt,如下:
1
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
  • 在app module的build.gradle中应用apt插件,并设置apt生成的索引的包名和类名,如果不设置的话在编译时会报错。
1
2
3
4
5
6
7
apply plugin: 'com.neenbedankt.android-apt'
apt {
arguments {
eventBusIndex "wang.imallen.eventbusannotationsample.MyEventBusIndex"
}
}
  • 最后,需要在app module的dependencies中引入EventBusAnnotationProcessor:
1
apt 'org.greenrobot:eventbus-annotation-processor:3.0.1'

完成以上几步后,重新编译一次,即可在app/build/generated/source/apt/debug/下看到生成的MyEventBusIndex类,如下是我的示例中生成的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/** This class is generated by EventBus, do not edit. */
public class MyEventBusIndex implements SubscriberInfoIndex {
private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX;
static {
SUBSCRIBER_INDEX = new HashMap<Class<?>, SubscriberInfo>();
putIndex(new SimpleSubscriberInfo(ThirdActivity.class, true, new SubscriberMethodInfo[] {
new SubscriberMethodInfo("onStickyEvent", wang.imallen.eventbusannotationsample.bean.UserInfo.class,
ThreadMode.MAIN, 0, true),
}));
putIndex(new SimpleSubscriberInfo(MainActivity.class, true, new SubscriberMethodInfo[] {
new SubscriberMethodInfo("onEvent", wang.imallen.eventbusannotationsample.simple.Event.class,
ThreadMode.MAIN),
new SubscriberMethodInfo("onEventPosting",
wang.imallen.eventbusannotationsample.threadmode.SecondEvent.class),
new SubscriberMethodInfo("onEventBackground",
wang.imallen.eventbusannotationsample.threadmode.ThirdEvent.class, ThreadMode.BACKGROUND),
new SubscriberMethodInfo("onEventAsync",
wang.imallen.eventbusannotationsample.threadmode.FourthEvent.class, ThreadMode.ASYNC),
}));
}
private static void putIndex(SubscriberInfo info) {
SUBSCRIBER_INDEX.put(info.getSubscriberClass(), info);
}
@Override
public SubscriberInfo getSubscriberInfo(Class<?> subscriberClass) {
SubscriberInfo info = SUBSCRIBER_INDEX.get(subscriberClass);
if (info != null) {
return info;
} else {
return null;
}
}
}

需要注意的是,项目中至少要有一个订阅信息,否则EventBusProcessor获取到的订阅信息为空,自然不生成相应的类了。

重新编译之后,在第一次使用EventBus之前(如Application或SplashActivity中),添加如下代码,以使Index生效:

1
EventBus eventBus=EventBus.builder().addIndex(new MyEventBusIndex()).build();

至于EventBus中常规事件和sticky事件的发布和订阅,都是非常简单的事情,也不是本文的重点,故不再赘述,就有一点需要注意,EventBus 3中sticky events的订阅是在注解中添加类似@Subscriber(sticky=true,threadMode=ThreadMode.MAIN)的属性.

读者可以fork我在github中的这个示例进行了解: EventBusProcessor使用示例

需要注意的是,如果不利用EventBusAnnotationProcessor,则EventBus 3的解析速度反而会比之前版本更慢。如下是square发布的EventBus3与之前版本在Nexus One,Nexus 5,Nexus 9上的表现对比:

显然,在不同版本的设备上,EventBus 3的速度都要快很多,是EventBus 2.4的3倍以上.

至于MyEventBusIndex是如何使订阅加速的,后面在分析源码时会提到。

2.EventBus 3.0.0架构

EventBus 3.0.0的发布和订阅事件的架构如下:

仍然是在某个线程发布后,通过EventBus分发到不同线程中的Subscriber,这一点与之前版本的并无不同。

EventBus 3.0.0的UML图如下:

需要重点关注的类有EventBus,SubscriberMethodFinder,FindState,SubscriberInfo,PostingThreadState,HandlerPoster,BackgroundPoster,
AsyncPoster,PendingPost,Subscription,SubscriberMethod,后面的分析也主要与这些类有关.

3.事件订阅/解除订阅

下面的代码讲解将结合 EventBusProcessor使用示例 进行。

事件订阅的代码如下:

1
2
3
4
5
6
7
8
9
10
11
public void register(Object subscriber) {
//subscriberClass是类似wang.imallen.eventbusannotationsample.MainActivity这样的
Class<?> subscriberClass = subscriber.getClass();
//subscriberMethods是类似MainActivity中的onEvent(),onEventAsync(),onEventBackground(),onEventMain(),onEventPosting(),onStickyEvent()这样的
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}

subscriberClass就是订阅者所属的Class,如MainActivity.class,之后利用subscriberMethodFinder查找subscriberClass中的订阅方法,其中findSubscriberMethods()方法如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) {
return subscriberMethods;
}
//ignoreGeneratedIndex默认为false,因为反射成本高
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;
}
}

由于反射成本高,而且EventBus 3引入了EventBusAnnotationProcessor,故默认ignoreGeneratedIndex为false,需要注意的是,如果设置ignoreGeneratedIndex为true,则前面使用的MyEventBusIndex无效,还是会走反射解析的分支。

要证实这一点很简单,进入findUsingReflection()方法看一下即可:

3.1 使用反射获取订阅信息

1
2
3
4
5
6
7
8
9
private List<SubscriberMethod> findUsingReflection(Class<?> subscriberClass) {
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
findUsingReflectionInSingleClass(findState);
findState.moveToSuperclass();
}
return getMethodsAndRelease(findState);
}

调用的是findUsingReflectionInSingleClass(),其代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
private void findUsingReflectionInSingleClass(FindState findState) {
Method[] methods;
try {
// This is faster than getMethods, especially when subscribers are fat classes like Activities
//methods是所有声明的方法,不包括只在基类的方法,如MainActivity中就是onCreate(),onDestroy(),onEvent(),onEventAsync(),onEventBackground(),onEventMain(),onEventPosting(),onStickEvent(),openSecondActivity()
methods = findState.clazz.getDeclaredMethods();
} catch (Throwable th) {
// Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149
methods = findState.clazz.getMethods();
findState.skipSuperClasses = true;
}
for (Method method : methods) {
int modifiers = method.getModifiers();
//必须是public和非static,非abstract,非bridge,非synthetic的方法
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
Class<?>[] parameterTypes = method.getParameterTypes();
if (parameterTypes.length == 1) {
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
if (subscribeAnnotation != null) {
//evnetType是类似wang.imallen.eventbusample.simple.Event这样的事件类型
Class<?> eventType = parameterTypes[0];
if (findState.checkAdd(method, eventType)) {
ThreadMode threadMode = subscribeAnnotation.threadMode();
findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
}
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException("@Subscribe method " + methodName +
"must have exactly 1 parameter but has " + parameterTypes.length);
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException(methodName +
" is a illegal @Subscribe method: must be public, non-static, and non-abstract");
}
}
}

代码逻辑很简单,就是先获取这个类的declared方法,然后选择其中是public和非static,非abstract,非bridge,非synthetic的方法,如果该方法的注解为Subscribe,则说明它是订阅事件的方法,解析注解参数,最后将解析结果到findState.subscriberMethods中.
里面有个小细节就是Class<?>eventType=parameterTypes[0],这意味着即使订阅方法中有多个参数,也只取第一个,如果确认该订阅对象中还未添加该eventtype的方法,则添加到findState.subscriberMethods中,其中checkAdd()方法的代码很简单,不展开分析了。
从这里引出一个细节:在一个订阅对象中,同一个事件类型只能有一个回调。

3.2 使用EventBusAnnotationProcessor解析结果获取订阅信息

回到findSubscriberMethods()这个方法,ignoreGeneratedIndex为false时,通过findUsingInfo()方法来获取订阅信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
//使用while而不是if的原因是findState.moveToSuperclass()会切换findState.clazz对象为基类Class对象,从而循环解析
while (findState.clazz != null) {
findState.subscriberInfo = getSubscriberInfo(findState);
if (findState.subscriberInfo != null) {
SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods();
for (SubscriberMethod subscriberMethod : array) {
if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
findState.subscriberMethods.add(subscriberMethod);
}
}
} else {
//如果发现获取不到subscriberInfo的话,就还是要使用反射来获取
findUsingReflectionInSingleClass(findState);
}
findState.moveToSuperclass();
}
return getMethodsAndRelease(findState);
}

进入getSubscriberInfo()查看:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
private SubscriberInfo getSubscriberInfo(FindState findState) {
if (findState.subscriberInfo != null && findState.subscriberInfo.getSuperSubscriberInfo() != null) {
SubscriberInfo superclassInfo = findState.subscriberInfo.getSuperSubscriberInfo();
if (findState.clazz == superclassInfo.getSubscriberClass()) {
return superclassInfo;
}
}
if (subscriberInfoIndexes != null) {
for (SubscriberInfoIndex index : subscriberInfoIndexes) {
SubscriberInfo info = index.getSubscriberInfo(findState.clazz);
if (info != null) {
return info;
}
}
}
return null;
}

这里由于subscriberInfo为null,故执行的是第二个分支,即在subscriberInfoIndexes中寻找,那这个subscriberInfoIndexes来自哪里呢?
可以看到,它是在constructor中被赋值的,而subscriberMethodFinder就在EventBus的constructor中创建的,这个subscriberInfoIndexes来自builder.subscriberInfoIndexes,而EventBusBuilder中的subscriberInfoIndexes就是来自于第一节中我们提到的如下代码:

1
EventBus eventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();

所以,MyEventBusIndex起作用的地方是在SubscriberMethodFinder中,结合MyEventBusIndex的代码可知,EventBusAnnotationProcessor解析出订阅信息,之后以订阅对象的Class为key放入到HashMap中(有的例外的是sticky events,这个在后面会分析),然后在SubscriberMethodFinder的getSubscriberInfo()中,根据findState.clazz来解析已有的结果,如果查找到了则直接返回。

回到findUsingInfo()中,仍然是要检查是否已添加同类型事件的回调,如果没有添加才会添加到findState.subscriberMethods中.

需要注意的是else中findUsingReflectionInSingleClass(findState),我个人认为是冗余代码,除非EventBusAnnotationProcessor解析出错或不完整,某种程度上是对EventBusAnnotationProcessor不自信的表现,期待作者在后面去掉吧。

注意return语句之前的findState.moveToSuperclass()这句,它其实是将findState中的clazz对象换成基类的,也就是说,事件订阅是可以继承的,之后循环之前的过程。

3.3 绑定订阅对象和方法

回到EventBus#register()中,在获取subscriberMethods之后,就是遍历各订阅方法,逐个绑定。subscribe()的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
//eventType是类似wang.imallen.eventbusannotationsample.simpel.Event这样的
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();
//注意这里是i<=size,是为了考虑到subscriptions为空的情况也能添加newSubscription到subscriptions中
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<>();
//subscriber是类似MainActivity,而subscribedEvents是类似MainActivity中涉及到的所有eventType,如Event.class,FirstEvent.class,SecondEvent.class,...,UserInfo.class
typesBySubscriber.put(subscriber, subscribedEvents);
}
subscribedEvents.add(eventType);
if (subscriberMethod.sticky) {
//eventInheritance表示是否要考虑继承
if (eventInheritance) {
// Existing sticky events of all subclasses of eventType have to be considered.
// Note: Iterating over all events may be inefficient with lots of sticky events,
// thus data structure should be changed to allow a more efficient lookup
// (e.g. an additional map storing sub classes of super classes: Class -> List<Class>).
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();
//给这个新的订阅者发送所有eventType类型或其子类的事件
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
} else {
//如果不考虑继承,则只要给新的订阅者发送eventType这个类型的最近一次的粘性事件
Object stickyEvent = stickyEvents.get(eventType);
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
}

这个方法略长,分为以下几步分析:

  • 首先根据subscriber和subscriberMethod生成一个Subscription对象
  • 之后从subscriptionsByEventType这个Map中获取对应eventType的所有Subscription对象,如果为空则新建,否则判断是否已经包含newSubscription,如包含则抛出已订阅的异常
  • 遍历subscriptions,如果发现subscriberMethod的优先级比某个Subscription对象中的优先级高,则用当前的Subscription对象取代它,之后跳出循环
  • typesBySubscriber是以订阅对象为key,事件类型列表为value的Map,将当前的事件类型添加到对应的List中即可
  • 最后一部分比较特殊,如果订阅的是粘性事件,则要分两种情况:
    • 如果要考虑事件继承(EventBusBuilder中默认为true),则需要遍历stickyEvents这个以事件的Class为key,事件本身为value的Map,如果发现某个事件类型是eventType的子类(就是eventType.isAssignableFrom(candidateEventType)的含义),则要发布这个事件类型对应的事件(也就是该事件类型最近一次事件)给这个订阅方法。至于事件发布的代码,在第4节会分析。
    • 如果不考虑事件继承,则只需要将eventType最近一次的事件(如果有的话)发送给这个订阅方法即可

3.4 解除订阅

解除订阅的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
/** Unregisters the given subscriber from all event classes. */
public synchronized void unregister(Object subscriber) {
List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
if (subscribedTypes != null) {
for (Class<?> eventType : subscribedTypes) {
unsubscribeByEventType(subscriber, eventType);
}
typesBySubscriber.remove(subscriber);
} else {
Log.w(TAG, "Subscriber to unregister was not registered before: " + subscriber.getClass());
}
}

代码很简单,就是解除与该订阅对象关联的所有事件类型.unsubscribeByEventType()的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/** Only updates subscriptionsByEventType, not typesBySubscriber! Caller must update typesBySubscriber. */
private void unsubscribeByEventType(Object subscriber, Class<?> eventType) {
List<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions != null) {
int size = subscriptions.size();
for (int i = 0; i < size; i++) {
Subscription subscription = subscriptions.get(i);
if (subscription.subscriber == subscriber) {
subscription.active = false;
subscriptions.remove(i);
i--;
size--;
}
}
}
}

该方法就是根据事件类型获取所有的订阅信息,如果该订阅信息的订阅对象为当前订阅对象,则将其移除.

解除订阅的逻辑很简单,就不画流程图了.

3.5 事件订阅总结

至此,事件订阅就基本分析完了,我们可以从中梳理出如下流程:

其中红线标出的是我认为不合理的地方。

4.事件发布

4.1 发布普通事件

post()的代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/** Posts the given event to the event bus. */
public void post(Object event) {
PostingThreadState postingState = currentPostingThreadState.get();
List<Object> eventQueue = postingState.eventQueue;
eventQueue.add(event);
if (!postingState.isPosting) {
postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();
postingState.isPosting = true;
if (postingState.canceled) {
throw new EventBusException("Internal error. Abort state was not reset");
}
try {
while (!eventQueue.isEmpty()) {
postSingleEvent(eventQueue.remove(0), postingState);
}
} finally {
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
  • 首先获取当前线程的PostingThreadState对象,实现方式是ThreadLocal
  • 然后将当前event添加到postingState的事件队列中
  • 如果当前线程没有正在发布事件,则遍历当前线程的事件队列,将事件逐个发布出去,其中postSingleEvent()的代码如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
Class<?> eventClass = event.getClass();
boolean subscriptionFound = false;
if (eventInheritance) {
List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
int countTypes = eventTypes.size();
for (int h = 0; h < countTypes; h++) {
Class<?> clazz = eventTypes.get(h);
subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);
}
} else {
subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);
}
if (!subscriptionFound) {
if (logNoSubscriberMessages) {
Log.d(TAG, "No subscribers registered for event " + eventClass);
}
if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
eventClass != SubscriberExceptionEvent.class) {
post(new NoSubscriberEvent(this, event));
}
}
}

这里涉及到事件继承的问题(默认情况下eventInheritance为true,即要考虑事件继承),如果考虑事件继承,则要获取这个事件类型的所有基类和实现的接口,并且还要将基类的基类,以及接口的基类也包含进去。如下是lookupAllEventTypes()的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/** Looks up all Class objects including super classes and interfaces. Should also work for interfaces. */
private static List<Class<?>> lookupAllEventTypes(Class<?> eventClass) {
synchronized (eventTypesCache) {
List<Class<?>> eventTypes = eventTypesCache.get(eventClass);
if (eventTypes == null) {
eventTypes = new ArrayList<>();
Class<?> clazz = eventClass;
while (clazz != null) {
eventTypes.add(clazz);
addInterfaces(eventTypes, clazz.getInterfaces());
clazz = clazz.getSuperclass();
}
eventTypesCache.put(eventClass, eventTypes);
}
return eventTypes;
}
}

代码很简单,就是循环获取基类以及基类的接口,并且将符合条件的事件类型添加到eventTypes中,最后将(eventClass,eventTypes)放入eventTypesCache中。

再回到postSingEvent()中,下面就是遍历eventTypes了,进入postSingleEventForEventType()中,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
subscriptions = subscriptionsByEventType.get(eventClass);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
for (Subscription subscription : subscriptions) {
postingState.event = event;
postingState.subscription = subscription;
boolean aborted = false;
try {
postToSubscription(subscription, event, postingState.isMainThread);
aborted = postingState.canceled;
} finally {
postingState.event = null;
postingState.subscription = null;
postingState.canceled = false;
}
if (aborted) {
break;
}
}
return true;
}
return false;
}

如果eventClass对应的订阅信息不为空,则将当前事件逐个发布到各订阅对象中,真正的发布处理在postToSubscription()中,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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);
}
}

显然,需要分线程模式为POSTING,MAIN,BACKGROUND,ASYNC这四种情况处理。

  • 如果是POSTING模式,则直接回调事件订阅方法,说明POSTING模式代表发布和订阅回是在同一个线程;
  • 如果要在主线程回调,而发布线程就是主线程的话,则直接回调;否则需要利用主线程发布代理(mainThreadPoster)来进行发布;
  • 如果要在后台线程中回调,而发布线程是主线程的话,则需要利用后台线程发布代理(backgroundPoster)来进行发布,否则直接发布;
  • 如果是异步发布,则不管当前是在什么线程,都是利用异步发布代理(asyncPoster)来进行发布
  • 如果threadMode不是其中之一的话,则抛出异常.

到这里为止,普通事件的发布就基本梳理完了。

4.2 发布sticky event

粘性事件的发布代码如下:

1
2
3
4
5
6
7
8
9
10
11
/**
* Posts the given event to the event bus and holds on to the event (because it is sticky). The most recent sticky
* event of an event's type is kept in memory for future access by subscribers using {@link Subscribe#sticky()}.
*/
public void postSticky(Object event) {
synchronized (stickyEvents) {
stickyEvents.put(event.getClass(), event);
}
// Should be posted after it is putted, in case the subscriber wants to remove immediately
post(event);
}

非常简单,相比普通事件的发布,就只是多了一步:将当前事件put到当前事件类型对应的实体中。这样做的目的是为了在绑定粘性事件回调时可以将最近一次该事件类型的事件发布给它。

至此,我们可以梳理出发布事件的流程: