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
- EventBus(三)-----从post这条线分析EventBus
- EventBus(二)------从register这条线分析EventBus
- EventBus的源码分析:Post流程
- EventBus从应用到源码分析
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- EventBus
- Eventbus
- python系列笔记3
- Windows平台的 PHP 报错 Fatal error: Class COM not found in 的解决方法
- linux——进程间的几种通信
- POJ2342(树形dp)
- 视屏快进快退按小片段进行
- EventBus(三)-----从post这条线分析EventBus
- 关于梯度消失,梯度爆炸的问题
- Hdfs block
- 关于crontab的坑
- centos安装netcat工具及测试
- RPC 工具 --Thrift(二) Thrift 异步模式
- 编程珠玑第8章总结
- Unity3d扫描 二维码 解析尝试
- 介绍图片的三种格式:GIF、JPEG、PNG