就从原来没有度过的几个函数开始吧!
1、postSticky(Object)
/**
* 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);
}
先把事件放到stickyEvents中存储,然后再调用常规的post
可以看出,每种类型的事件最多缓存一个。
而在读注册的时候,会检查是否有sticky事件。如果有则处理。
sticky事件是先发生事件,后来才有相应的方法处理。
而常规的事件是现有处理方法,后发生事件。
2、getStickyEvent
/** * Gets the most recent sticky event for the given type. * * @see #postSticky(Object) */ public <T> T getStickyEvent(Class<T> eventType) { synchronized (stickyEvents) { return eventType.cast(stickyEvents.get(eventType)); } }从代码中看,也没有什么好说的,就是获取缓存中存在的某一种类型的事件。
3、removeStickyEvent(Class)
/** * Remove and gets the recent sticky event for the given event type. * * @see #postSticky(Object) */ public <T> T removeStickyEvent(Class<T> eventType) { synchronized (stickyEvents) { return eventType.cast(stickyEvents.remove(eventType)); } }
删除存在的某类事件4、 removeStickyEvent(Object)
/** * Removes the sticky event if it equals to the given event. * * @return true if the events matched and the sticky event was removed. */ public boolean removeStickyEvent(Object event) { synchronized (stickyEvents) { Class<?> eventType = event.getClass(); Object existingEvent = stickyEvents.get(eventType); if (event.equals(existingEvent)) { stickyEvents.remove(eventType); return true; } else { return false; } } }删除某个缓存的事件