この記事では、原則の実現する方法を説明するために、ソースコードを解析するために、メインEventBus一般的に用いられている方法から始まります。
基本的な使用
以下のためにサインアップ
EventBus.getDefault().register(this);
イベント処理
@Subscribe(threadMode = ThreadMode.MainThread)
public void onNewsEvent(NewsEvent event){
String message = event.getMessage();
mTv_message.setText(message);
}
公開イベント
EventBus.getDefault().post(new NewsEvent("我是来自SecondActivity的消息,你好!"));
これらはEventBusの基本的な使用されています。私たちは、のgetDefaultについて話し始めます。
getDefault()
static volatile EventBus defaultInstance;
public static EventBus getDefault() {
if (defaultInstance == null) {
synchronized (EventBus.class) {
if (defaultInstance == null) {
defaultInstance = new EventBus();
}
}
}
return defaultInstance;
}
上記のコードによって知ることができるのgetDefault()機構がEventBusビス(DCL)機構によるロックをチェックし、単一の実施形態において実装され、デフォルト設定はEventBusオブジェクトが得られます。
ここでは、レジスタ()メソッドを見続けています。
登録()
レジスタを見る前に()、我々はいくつかの重要なEventBusメンバ変数を見てする必要があります。以下の内容の理解を容易にするために。
/** Map<订阅事件, 订阅该事件的订阅者集合> */
private final Map<Class<?>, CopyOnWriteArrayList<Subscription>> subscriptionsByEventType;
/** Map<订阅者, 订阅事件集合> */
private final Map<Object, List<Class<?>>> typesBySubscriber;
/** Map<订阅事件类类型,订阅事件实例对象>. */
private final Map<Class<?>, Object> stickyEvents;
ここで()のコード実行は、特定のレジスタに依存します。
public void register(Object subscriber) {
//订阅者类型
Class<?> subscriberClass = subscriber.getClass();
//判断该类是不是匿名类,如果是匿名累要使用反射
boolean forceReflection = subscriberClass.isAnonymousClass();
//获取订阅者全部的响应函数信息(即上面的onNewsEvent()之类的方法)
List<SubscriberMethod> subscriberMethods =
subscriberMethodFinder.findSubscriberMethods(subscriberClass, forceReflection);
//循环每一个事件响应函数,执行 subscribe()方法,更新订阅相关信息
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
このように、レジスタ()クラスタイプの加入者への第一歩。第2のステップは、全ての第三の取得ステップに応答して、加入者クラスSubscriberMethodFinderクラス、関数のセットを解決するために、トラバーサルが実行される機能は、購読()メソッドサブスクリプション関連の情報を更新します。
subscriberMethodFinderについて、ここで提示されていません。最初の手がかりに従うと、サブスクライブ()メソッドを見続けます。
3つのステップで機能をサブスクライブします。
最初のステップ:
//获取订阅的事件类型
Class<?> eventType = subscriberMethod.eventType;
//获取订阅该事件的订阅者集合
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
//把通过register()订阅的订阅者包装成Subscription 对象
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
//订阅者集合为空,创建新的集合,并把newSubscription 加入
if (subscriptions == null) {
subscriptions = new CopyOnWriteArrayList<Subscription>();
subscriptionsByEventType.put(eventType, subscriptions);
} else {
//集合中已经有该订阅者,抛出异常。不能重复订阅
if (subscriptions.contains(newSubscription)) {
throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
+ eventType);
}
}
//把新的订阅者按照优先级加入到订阅者集合中。
synchronized (subscriptions) {
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;
}
}
}
ステップ2:
//根据订阅者,获得该订阅者订阅的事件类型集合
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
//如果事件类型集合为空,创建新的集合,并加入新订阅的事件类型。
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<Class<?>>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
//如果事件类型集合不为空,加入新订阅的事件类型
subscribedEvents.add(eventType);
第三段階:
//该事件是stick=true。
if (subscriberMethod.sticky) {
//响应订阅事件的父类事件
if (eventInheritance) {
Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
//循环获得每个stickyEvent事件
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加入者キューsubscriptionsByEventTypeに現在の加入者情報の優先度に応じて、そのイベントタイプのメッセージキューのすべてのサブスクライバによって得られた。
ステップ2:加入者に現在のすべてのtypesBySubscriberにサブスクライブイベントキューは、イベントキューは、その後の退会でこれを保存typesBySubscriber;
ステップ3:イベントはイベントキューの最後のイベントの種類は、現在削除に加入stickyEventsから保存されている場合、このイベントはスティッキーイベントは、あるかどうかをチェック人。
これは、サブスクリプション機能を完了します。
登録解除()
public synchronized void unregister(Object subscriber) {
// 获取该订阅者所有的订阅事件类类型集合.
List<Class<?>> subscribedTypes = typesBySubscriber.get(subscriber);
if (subscribedTypes != null) {
for (Class<?> eventType : subscribedTypes) {
unsubscribeByEventType(subscriber, eventType);
}
// 从typesBySubscriber删除该<订阅者对象,订阅事件类类型集合>
typesBySubscriber.remove(subscriber);
} else {
Log.e("EventBus", "Subscriber to unregister was not registered before: "
+ subscriber.getClass());
}
}
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 --;
}
}
}
}
登録解除()メソッドは、比較的単純であり、主に終了した同期typesBySubscriber subscriptionsByEventType二組。
役職()
public void post(Object event) {
// 获取当前线程的Posting状态.
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 {
// 循环处理当前线程eventQueue中的每一个event对象.
while (!eventQueue.isEmpty()) {
postSingleEvent(eventQueue.remove(0), postingState);
}
} finally {
// 处理完知乎重置postingState一些标识信息.
postingState.isPosting = false;
postingState.isMainThread = false;
}
}
}
ポスト機能はまず、ポスト情報PostingThreadStateイベントキューが含まれている現在のスレッドを、取得そのイベントキューに現在のイベントを追加し、サイクル呼び出しpostSingleEvent機能は、各イベントキューを公開します。
private void postSingleEvent(Object event, PostingThreadState postingState) {
//分发事件的类型
Class<?> eventClass = event.getClass();
boolean subscriptionFound = false;
//响应订阅事件的父类事件
if (eventInheritance) {
//找出当前订阅事件类类型eventClass的所有父类的类类型和其实现的接口的类类型
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);
}
....................................
}
イベントごとに、各加入者に発行されPostSingleEventForEventType関数呼び出し。
private boolean postSingleEventForEventType(Object event, PostingThreadState postingState,
Class<?> eventClass) {
CopyOnWriteArrayList<Subscription> subscriptions;
synchronized (this) {
// 获取订阅事件类类型对应的订阅者信息集合.(register函数时构造的集合)
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;
}
各加入者にイベントを投稿するpostToSubscription関数を呼び出します。
postToSubscription機能はどのモードで実行イベント応答関数の下で決定するために、ThreadMode加入者を判断します。ここでは、ソースコードを掲示していません。フォローアップはまだ開始されていない、ここで反射し、スレッドのスケジューリング問題に保持します。
2019年には、科学の進歩のAndroid方法、方法がない、多くの人が個人的に私に尋ねる、7年間のAndroidエンジニアの開発に従事していますか?
はい、最初に私は、高度なAndroidの開発を強化したい人たちを助けることを望んで、学習教材を整理するヶ月以上を費やしたが、どのように高度な学習の友人を知りません。[ 高度なUI、パフォーマンスの最適化、建築コース、NDK、Kotlin、ハイブリッド開発(ReactNative + Weex)、フラッターやその他の技術情報アーキテクチャを含むあなたの前のインタビューを見直し助け、良い仕事を見つけるだけでなく、保存することを望んで、]時間に彼らが学ぶためにオンラインで情報を検索します。