EventBus(三)-----从post这条线分析EventBus

来源:互联网 发布:c语言在线翻译器 编辑:程序博客网 时间:2024/05/20 09:10

这篇笔记主要是从post方法入手,学习EventBus.
post(event)

postSingleEvent(eventQueue.remove(0), isMainThread);

private void postSingleEvent(Object event, boolean isMainThread)

 private void postSingleEvent(Object event, boolean isMainThread) throws Error {        Class<? extends Object> eventClass = event.getClass();        List<Class<?>> eventTypes = findEventTypes(eventClass);        boolean subscriptionFound = false;        int countTypes = eventTypes.size();        for (int h = 0; h < countTypes; h++) {            Class<?> clazz = eventTypes.get(h);            CopyOnWriteArrayList<Subscription> subscriptions;            synchronized (this) {                subscriptions = subscriptionsByEventType.get(clazz);            }            if (subscriptions != null) {                for (Subscription subscription : subscriptions) {                    postToSubscription(subscription, event, isMainThread);                }                subscriptionFound = true;            }        }        if (!subscriptionFound) {            Log.d(TAG, "No subscripers registered for event " + eventClass);            if (eventClass != NoSubscriberEvent.class && eventClass != SubscriberExceptionEvent.class) {                post(new NoSubscriberEvent(this, event));            }        }    }

从上述代码可以看到,eventType(包括它的父类和接口)从subscriptionsByEventType映射到订阅信息集合subscriptions = subscriptionsByEventType.get(clazz);然后执行方法postToSubscription(subscription, event, isMainThread);

private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {        switch (subscription.subscriberMethod.threadMode) {        case PostThread:            invokeSubscriber(subscription, event);//直接在本线程执行            break;        case MainThread:            if (isMainThread) {                invokeSubscriber(subscription, event);            } else {                mainThreadPoster.enqueue(subscription, event);            }            break;        case BackgroundThread:            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);        }    }

由上述代码确实可以看到—-ThreadMode决定了事件处理方法被调用的线程(在笔记一中已经说过)。

接下来,在看一个方法invokeSubscriber(Subscription subscription, Object event)
用了java的反射机制。

try {            subscription.subscriberMethod.method.invoke(subscription.subscriber, event);        } catch (InvocationTargetException e) {            Throwable cause = e.getCause();            if (event instanceof SubscriberExceptionEvent) {                // Don't send another SubscriberExceptionEvent to avoid infinite event recursion, just log                Log.e(TAG, "SubscriberExceptionEvent subscriber " + subscription.subscriber.getClass()                        + " threw an exception", cause);                SubscriberExceptionEvent exEvent = (SubscriberExceptionEvent) event;                Log.e(TAG, "Initial event " + exEvent.causingEvent + " caused exception in "                        + exEvent.causingSubscriber, exEvent.throwable);            } else {                if (logSubscriberExceptions) {                    Log.e(TAG, "Could not dispatch event: " + event.getClass() + " to subscribing class "                            + subscription.subscriber.getClass(), cause);                }                SubscriberExceptionEvent exEvent = new SubscriberExceptionEvent(this, cause, event,                        subscription.subscriber);                post(exEvent);            }        } catch (IllegalAccessException e) {            throw new IllegalStateException("Unexpected exception", e);        }    }
0 0
原创粉丝点击