EventBus原始碼解析二

2021-08-19 09:40:56 字數 1704 閱讀 4523

上次分析了 註冊  這次分析下 事件傳送

eventbus.getdefault().post(new anyeventtype().setresult("aaa"));

public void post(object event)

try

} finally }}

eventqueue.add(event);  新增到事件佇列中,

然後檢視postsingleevent 方法

postsingleevent(eventqueue.remove(0), postingstate);

private void postsingleevent(object event, postingthreadstate postingstate) throws error

} else

if (!subscriptionfound)

if (sendnosubscriberevent && eventclass != nosubscriberevent.class &&

eventclass != subscriberexceptionevent.class)

}這裡主要看

private boolean postsingleeventforeventtype(object event, postingthreadstate postingstate, class<?> eventclass)

if (subscriptions != null && !subscriptions.isempty()) finally

if (aborted)

}return true;

}return false;

list> eventtypes = lookupalleventtypes(eventclass);

返回其他activitu type 和這個type 一樣的方法

subscriptions = subscriptionsbyeventtype.get(eventclass);

這裡返回訂閱列表

posttosubscription(subscription, event, postingstate.ismainthread);

private void posttosubscription(subscription subscription, object event, boolean ismainthread) else

break;

case background:

if (ismainthread) else

break;

case async:

asyncposter.enqueue(subscription, event);

break;

default:

throw new illegalstateexception("unknown thread mode: " + subscription.subscribermethod.threadmode);

}這裡看invokesubscriber

void invokesubscriber(subscription subscription, object event) catch (invocationtargetexception e) catch (illegalacces***ception e)

這裡通過反射呼叫訂閱列表中 訂閱的訪問 ,整個流程完成

EventBus原始碼解析(二)

那麼我們就馬上進入主題吧,上篇文章分析到boolean sticky這個引數和registersticky object subscriber 這個方法,這個方法與之前我們註冊的方法是不是很像啊?public void register object subscriber public void r...

EventBus3 0原始碼解析

eventbus的註冊 eventbus.getdefault register this 那麼我們來看一下eventbus.getdefault 到底做了一下什麼操作 public static eventbus getdefault return defaultinstance 就是拿到了eve...

EventBus原始碼解析01 獲取例項

通常,我們會通過getdefault 來獲取eventbus的例項。eventbus.getdefault 我們來檢視他內部的實現。public static eventbus getdefault return defaultinstance 可以看到,這是個雙重校驗鎖的單例模式,說明通過getd...