EventBus的使用捐下,以及源碼分析
EventBus的使用
EventBus能夠簡化各組件間的通信到腥,能夠有效的分離事件的發(fā)送方和接收方朵逝,能避免復(fù)雜和容易出錯的依賴性和生命周期的問題。
EventBus的三要素
- Event事件左电,可以是任意類型廉侧。
- Subscriber事件訂閱者页响,需要進(jìn)行注冊篓足。
- Publisher事件的發(fā)布者。
EventBus的用法
添加依賴
implementation 'org.greenrobot:eventbus:3.1.1'
注冊事件
override fun onStart() {
EventBus.getDefault().register(this)
super.onStart()
}
當(dāng)我們需要在Activity或Fragment里訂閱事件的時候闰蚕,我們需要注冊EventBus栈拖。我們一般選擇在onStart()里去注冊。onStop()里去解除没陡。
解除事件
override fun onStop() {
EventBus.getDefault().unregister(this)
super.onStop()
}
解除事件一定需要涩哟,為了防止內(nèi)存泄漏。
發(fā)送事件
EventBus.getDefault().post("發(fā)送EventBus事件盼玄,任何類型都可以贴彼。")
處理事件
@Subscribe(threadMode = ThreadMode.MAIN)
fun MessageEventPost(message: String) {
//todo
}
EventBus注解參數(shù)
注解代碼
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Subscribe {
ThreadMode threadMode() default ThreadMode.POSTING;
boolean sticky() default false;
int priority() default 0;
}
threadMode():運行在那個線程,枚舉類型埃儿。(POSTING,MAIN,MAIN_ORDERED,BACKGROUND,ASYNC)
sticky():是否是粘性事件器仗。
所謂粘性事件,就是在發(fā)送事件之后再訂閱該事件也能收到該事件童番。
priority():優(yōu)先級(0-100)
EventBus源碼分析
EventBus注冊代碼
public void register(Object subscriber) {
Class<?> subscriberClass = subscriber.getClass();
//通過subscriberMethodFinder 來找到訂閱者訂閱了哪些事件.返回一個 SubscriberMethod 對象的 List, SubscriberMethod里包含了這個方法的 Method 對象,
//以及將來響應(yīng)訂閱是在哪個線程的 ThreadMode ,以及訂閱的事件類型 eventType ,以及訂閱的優(yōu)先級 priority ,
//以及是否接收粘性 sticky 事件的 boolean 值精钮,其實就是解析這個類上的所有 Subscriber 注解方法屬性。
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
List<SubscriberMethod> findSubscriberMethods(Class<?> subscriberClass) {
//先在緩存中讀取
List<SubscriberMethod> subscriberMethods = METHOD_CACHE.get(subscriberClass);
if (subscriberMethods != null) {
return subscriberMethods;
}
//ignoreGeneratedIndex 默認(rèn)值為false剃斧,findUsingInfo(Class<?> subscriberClass)
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 {
//訂閱者Class存入緩存
METHOD_CACHE.put(subscriberClass, subscriberMethods);
return subscriberMethods;
}
}
private List<SubscriberMethod> findUsingInfo(Class<?> subscriberClass) {
//獲取FindState()
FindState findState = prepareFindState();
findState.initForSubscriber(subscriberClass);
while (findState.clazz != null) {
//findState.subscriberInf == null is true
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 {
//核心代碼
findUsingReflectionInSingleClass(findState);
}
findState.moveToSuperclass();
}
return getMethodsAndRelease(findState);
}
private void findUsingReflectionInSingleClass(FindState findState) {
Method[] methods;
try {
// 通過發(fā)射獲取訂閱類的所有方法
methods = findState.clazz.getDeclaredMethods();
} catch (Throwable th) {
methods = findState.clazz.getMethods();
findState.skipSuperClasses = true;
}
for (Method method : methods) {
//獲取類的修飾符
int modifiers = method.getModifiers();
//找到所有聲明為public的方法
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
//獲取參數(shù)的class
Class<?>[] parameterTypes = method.getParameterTypes();
//只可以包含一個參數(shù)
if (parameterTypes.length == 1) {
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
if (subscribeAnnotation != null) {
//獲取事件的Class轨香,也就是方法參數(shù)的Class
Class<?> eventType = parameterTypes[0];
if (findState.checkAdd(method, eventType)) {
// 獲取 ThreadMode
ThreadMode threadMode = subscribeAnnotation.threadMode();
// 往集合里面添加 SubscriberMethod ,解析方法注解所有的屬性
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");
}
}
}
執(zhí)行完subscriberMethodFinder.findSubscriberMethods(subscriberClass);會通過類對象的 class 去解析這個類中的所有 Subscribe 注解方法的所有屬性值幼东,一個注解方法對應(yīng)一個 SubscriberMethod 對象臂容,包括 threadMode科雳,priority,sticky策橘,eventType炸渡,method。效果如下圖:
接下來看subscribe這個方法丽已,該方法把subscriber蚌堵,SubscriberMethod分別存好,存入如下兩個集合:
//key 是 Event 參數(shù)的類 -> 上面的String.class
//value 是存放Subscription的集合 Subscription里面包含兩個類 一個是subscriber訂閱者(反射執(zhí)行對象)沛婴,一個是SubscriberMethod 注解方法的所有屬性參數(shù)值
private final Map<Class<?>, CopyOnWriteArrayList<Subscription>> subscriptionsByEventType;
// key 是所有的訂閱者
// value 是所有訂閱者里面方法的參數(shù)的 class吼畏,eventType
private final Map<Object, List<Class<?>>> typesBySubscriber;
subscribe方法如下:
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
//subscriber MainActivity eventType = String
Class<?> eventType = subscriberMethod.eventType;
//將MainActivity 和 SubscriberMethod 再次封裝
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
//獲取訂閱了此事件類的所有訂閱者列表
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions == null) {
//線程安全的ArrayList
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();
//處理優(yōu)先級
for (int i = 0; i <= size; i++) {
if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
subscriptions.add(i, newSubscription);
break;
}
}
//通過subscriber 獲取 List<Class<?>>
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
//將事件類加入 訂閱者事件列表中
subscribedEvents.add(eventType);
//粘性處理事件
if (subscriberMethod.sticky) {
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();
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
} else {
Object stickyEvent = stickyEvents.get(eventType);
checkPostStickyEventToSubscription(newSubscription, stickyEvent);
}
}
}
subscriptionsByEventType 集合架構(gòu)圖如下:
EventBus post代碼
遍歷剛剛的map,拿到map的內(nèi)容嘁灯,通過反射進(jìn)行方法的調(diào)用
public void post(Object event) {
//event:"發(fā)送EventBus事件泻蚊,任何類型都可以。"
//currentPostingThreadState 是一個 ThreadLocal
PostingThreadState postingState = currentPostingThreadState.get();
//獲取到線程獨有的變量
List<Object> eventQueue = postingState.eventQueue;
//將事件加入隊列
eventQueue.add(event);
if (!postingState.isPosting) {
//判斷是否是主線程
postingState.isMainThread = isMainThread();
postingState.isPosting = true;
if (postingState.canceled) {
throw new EventBusException("Internal error. Abort state was not reset");
}
try {
//循環(huán)遍歷隊列丑婿,發(fā)送單個事件調(diào)用postSingleEvent
while (!eventQueue.isEmpty()) {
postSingleEvent(eventQueue.remove(0), postingState);
}
} finally {
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
//獲取事件的Class String.class
Class<?> eventClass = event.getClass();
//是否找到訂閱者
boolean subscriptionFound = false;
// 是否支持事件繼承性雄,默認(rèn)true
if (eventInheritance) {
//查找eventClass 所有的父類和接口
List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
int countTypes = eventTypes.size();
for (int h = 0; h < countTypes; h++) {
//依次向eventClass的父類或接口的訂閱方法發(fā)送事件
Class<?> clazz = eventTypes.get(h);
subscriptionFound |= postSingleEventForEventType(event, postingState, clazz);
}
} else {
subscriptionFound = postSingleEventForEventType(event, postingState, eventClass);
}
if (!subscriptionFound) {
if (logNoSubscriberMessages) {
logger.log(Level.FINE, "No subscribers registered for event " + eventClass);
}
if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent.class &&
eventClass != SubscriberExceptionEvent.class) {
post(new NoSubscriberEvent(this, event));
}
}
}
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
//得到Subscription列表
subscriptions = subscriptionsByEventType.get(eventClass);
}
if (subscriptions != null && !subscriptions.isEmpty()) {
//遍歷 subscriptions
for (Subscription subscription : subscriptions) {
postingState.event = event;
postingState.subscription = subscription;
boolean aborted = false;
try {
//發(fā)送事件
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;
}
private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
// 根據(jù)不同的線程模式執(zhí)行對應(yīng)
switch (subscription.subscriberMethod.threadMode) {
//和發(fā)送事件處于同一線程
case POSTING:
invokeSubscriber(subscription, event);
break;
//主線程
case MAIN:
if (isMainThread) {
invokeSubscriber(subscription, event);
} else {
mainThreadPoster.enqueue(subscription, event);
}
break;
//子線程
case MAIN_ORDERED:
if (mainThreadPoster != null) {
mainThreadPoster.enqueue(subscription, event);
} else {
// temporary: technically not correct as poster not decoupled from subscriber
invokeSubscriber(subscription, event);
}
break;
//和發(fā)送事件處于不同線程
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);
}
}