EventBus笔记--源码--发送消息

上一节中提到了两个很重要的集合, 在注册的时候收集到了必要的信息 , 这一节我们来看看发送数据的具体实现

EventBus笔记--源码--发送消息_第1张图片

EventBus -- 基本使用
EventBus源码 -- 注册
EventBus源码 -- 发送消息
EventBus源码 -- 注销

EventBusd的发送消息

EventBus.getDefault().post(new MessageForFrag("来自activity的消息,你收到了么"));

  • getDefalt方法不再赘述 , 获取一个单例的实例
  • 查看post方法
public void post(Object event) {
        // 首先拿到事件队列 , 将要发送的事件加入到队列中  
        PostingThreadState postingState = currentPostingThreadState.get();
        List 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;
            }
        }
    }
 
 

发送单个事件的方法postSingleEvent

private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
         // 获取事件的类
        Class eventClass = event.getClass();
        boolean subscriptionFound = false;
        //eventInheritance表示是否向上查找事件的父类,默认为true
        if (eventInheritance) {
            List> 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找到所有的父类事件并存在List中,然后通过postSingleEventForEventType方法对事件逐一处理,接下来看看postSingleEventForEventType方法:

private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class eventClass) {
        CopyOnWriteArrayList 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;
    }

到这里我们又看到了熟悉的那个集合: subscriptionsByEventType , 通过这个集合找到了事件类型对应的所有方法 , 然后将事件信息和方法保存在postingState中 ,调用postToSubscription发送给订阅方法

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);
        }
    }

看到这里是不是熟悉了好多 ?四种线程模型的switch , 对应四种不同的处理, 无非就是加入队列和invokeSubscriber.
mainThreadPoster是HandlerPoster类型的继承自Handler,通过Handler将订阅方法切换到主线程执行。其他两种poster也都继承自Runnable接口 , 最终也都执行invokeSubscriber方法, 只是参数不一样了...这里拿MAIN中的方法来讲解.

void invokeSubscriber(Subscription subscription, Object event) {
        try {
            //通过反射获取到指定事件类型(参数)的订阅方法, 并调用
            subscription.subscriberMethod.method.invoke(subscription.subscriber, event);
        } catch (InvocationTargetException e) {
            handleSubscriberException(subscription, event, e.getCause());
        } catch (IllegalAccessException e) {
            throw new IllegalStateException("Unexpected exception", e);
        }
    }

至此, post传入的事件就被相对应的订阅方法所接收.

你可能感兴趣的:(EventBus笔记--源码--发送消息)