以前,对于 activity 和 fragment 之间的通信可以使用接口的方式,定义起来比较麻烦费事,偶然间发现可以使用 EventBus,发现很好用,查看了一下官方说明:EventBus 是针一款对 Android 的发布 / 订阅事件总线。它可以让我们很轻松的实现在 Android 各个组件之间传递消息,并且代码的可读性更好,耦合度更低。但是在用的过程中总会出现一些问题,下面就将出现的问题详细记录一下,顺便具体学习 EventBus(GreenRobot) 这个第三方开源库,了解它内部的实现原理,以至于出了问题可以快速定位修复。
官网: http://greenrobot.org/eventbus/documentation/
github: https://github.com/greenrobot/EventBus
以下使用都是基于 EventBus3.0。
对于 EventBus 的原理呢,可以参照一下官网的这张图:
具体的使用方法可以看官网,很简单,简单罗列一下:
Step 1: Add Gradle
- compile'org.greenrobot:eventbus:3.0.0'
Step 2: Define events
- public classMessageEvent {publicfinal String message;public MessageEvent(String message) {this.message = message;
- }
- }
Step 3: Prepare subscribers
Subscribers implement event handling methods (also called"subscriber methods") that will be called when an event is posted. These are defined with the @Subscribe annotation.Note that with EventBus 3 the method name can be chosen freely (no naming conventions like in EventBus 2).
- // This method will be called when a MessageEvent is posted (in the UI thread for Toast)@Subscribe(threadMode = ThreadMode.MAIN)public void onMessageEvent(MessageEventevent) {
- Toast.makeText(getActivity(),event.message, Toast.LENGTH_SHORT).show();
- }
Subscribers also need to register themselves to and unregister from the bus. Only while subscribers are registered, they will receive events. In Android, in activities and fragments you should usually register according to their life cycle:
- @Override
- public void onStart() {super.onStart();
- EventBus.getDefault().register(this);
- }@Override
- public void onStop() {
- EventBus.getDefault().unregister(this);super.onStop();
- }
Step 4: Post events
- EventBus.getDefault().post(new MessageEvent("Hello everyone!"));
以上便是官网给出的简单使用步骤,也不是很难,所以就不需要翻译了。接下来我们针对使用过程中出现的问题来进行一步一步的深入探究。
我们先来使用一个简单的例子来总结。这个例子主要有三个 activity,MainActivity、SecondActivity、ThirdActivity 以及一个 MessageEvent 对象。我们在 MainActivity、SecondActivity 中分别注册了 MessageEvent 事件,在 ThirdActivity 中 post MessageEvent 事件,这样我们在 MainActivity、SecondActivity 中应该都能接受到该事件。下面是具体的代码。
第一个 MainActivity
- public class MainActivity extends AppCompatActivity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- EventBus.getDefault().register(this);
- Button btn = (Button) findViewById(R.id.button2);
- btn.setOnClickListener(newView.OnClickListener() {@Override
- public void onClick(View v) {
- startActivity(newIntent(MainActivity.this, SecondActivity.class));
- }
- });
- }//接收事件
- @Subscribe(threadMode = ThreadMode.MAIN)public void fresh(MessageEvent messageEvent) {
- M2Log.d("MessageEvent -----------------> MainActivity");
- }@Override
- protected void onDestroy() {super.onDestroy();
- M2Log.d("MainActivity -----------------> onDestroy");
- EventBus.getDefault().unregister(this);
- }
- }
第二个 SecondActivity
- public class SecondActivity extends AppCompatActivity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main2);
- EventBus.getDefault().register(this);
- Button btn = (Button) findViewById(R.id.btn2);
- btn.setOnClickListener(newView.OnClickListener() {@Override
- public void onClick(View v) {
- startActivity(newIntent(SecondActivity.this, ThirdActivity.class));
- }
- });
- }//接收事件
- @Subscribe(threadMode = ThreadMode.MAIN)public void fresh(MessageEvent messageEvent) {
- M2Log.d("MessageEvent -----------------> SecondActivity");
- }@Override
- protected void onDestroy() {super.onDestroy();
- M2Log.d("SecondActivity -----------------> onDestroy");
- EventBus.getDefault().unregister(this);
- }
- }
第三个 ThirdActivity
- public class ThirdActivity extends AppCompatActivity{
- @Override
- protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main3);
- Button btn = (Button) findViewById(R.id.btn3);
- btn.setOnClickListener(newView.OnClickListener() {@Override
- public void onClick(View v) {//发送消息EventBus.getDefault().post(newMessageEvent(""));
- finish();
- }
- });
- }
- }
打印输出结果
很显然,MainActivity 和 SecondActivity 都接收到了 MessageEvent 事件。
细心的会发现,我们
EventBus.getDefault().register(this); EventBus.getDefault().unregister(this);
注册生命周期是放在 onCreate() 和 onDestroy() 中的,如果我们按照官网上来,放在 onStart() 和 onStop() 中,你就会发现,我们接收不到 MessageEvent 事件,可以验证一下
- @Override
- protected void onStart() {super.onStart();
- M2Log.d("SecondActivity -----------------> onStart");
- EventBus.getDefault().register(this);
- }@Override
- protected void onStop() {super.onStop();
- M2Log.d("SecondActivity -----------------> onStop");
- EventBus.getDefault().unregister(this);
- }
结果是什么都不会打印,所以我们一般会将注册生命周期放到 onCreate() 和 onDestroy() 中去。
我们在开发过程中,你会发现有的时候会出现问题:
1、没有注册该事件
出现这种情况,大多数是没有注册该事件,什么意思呢?就是下面的类似代码没有写。
- @Subscribe(threadMode = ThreadMode.MAIN)public void fresh(MessageEvent messageEvent) {
- M2Log.d("MessageEvent -----------------> MainActivity");
- }
有的人写了类似的注册代码,但还是会报这个错误,那就涉及到注册的生命周期了。
- @Override
- public void onStart() {super.onStart();
- EventBus.getDefault().register(this);
- }@Override
- public void onStop() {
- EventBus.getDefault().unregister(this);super.onStop();
- }
2、多次注册
这是我们在生命周期中注册该事件时多次注册造成的。解决方法很简单,可以判断一下
- //没有注册时再进行注册操作
- if(!EventBus.getDefault().isRegistered(this)){
- EventBus.getDefault().register(this);
- }
粘性事件类似于粘性广播,就是一次注册永久使用。
如何使用呢?类似于前面的,只不过加了一个 sticky = true,发送时采用 postSticky 而已
- //发布事件EventBus.getDefault().postSticky(newMessageEvent("Hello everyone!"));
在接收的时候添加一个 sticky = true 即可。
- //注册接收
- @Subscribe(threadMode = ThreadMode.MAIN,sticky =true)public void fresh(MessageEvent messageEvent) {
- M2Log.d("MessageEvent -----------------> SecondActivity");
- }@Override
- protected void onStart() {super.onStart();
- M2Log.d("SecondActivity -----------------> onStart");
- EventBus.getDefault().register(this);
- }@Override
- protected void onStop() {super.onStop();
- M2Log.d("SecondActivity -----------------> onStop");
- EventBus.getDefault().unregister(this);
- }
我们前面出现过一个问题,那就是我们在 onStart 和 onStop 中注册,接收不到 EventMessage,通过粘性事件,就可以解决这个问题。不过当你使用粘性事件时你会发现,每次进入注册该事件的 activity 中都会主动接收到该事件。
下面是我发送了一个粘性事件,我们在 MainActivity 和 SecondActivity 中会接收到该事件,我们退出 APP 后,再次进入,则又会接收到该事件。
清除粘性事件
- MessageEventstickyEvent =EventBus.getDefault().getStickyEvent(MessageEvent.class);// Bettercheck that an event was actually posted beforeif(stickyEvent != null) {// "Consume"the sticky eventEventBus.getDefault().removeStickyEvent(stickyEvent);// Now dosomething with it
- }
- /**
- * threadMode
- * 表示方法在什么线程执行 (Android更新UI只能在主线程, 所以如果需要操作UI, 需要设置ThreadMode.MainThread)
- *
- * sticky
- * 表示是否是一个粘性事件 (如果你使用postSticky发送一个事件,那么需要设置为true才能接受到事件)
- *
- * priority
- * 优先级 (如果有多个对象同时订阅了相同的事件, 那么优先级越高,会优先被调用.)
- * */
- @Subscribe(threadMode = ThreadMode.MainThread, sticky =true, priority =100)public void onEvent(MsgEvent event){
- }
上面便是 EventBus3.0 的常规用法,我们在知道了常规用法后还不行,必须深入了解一下它的内部实现原理,否则到时候出了问题后不知道该如何解决,要知其然而之所以然。下面我们便来分析一下它的源码。
源码解析部分主要从 register、post、以及 unregisger 这三部分进行分析。
我们首先从注册入手,先分析
- EventBus.getDefault()
进入源码:
- static volatileEventBus defaultInstance;/** Convenience singleton for apps using a process-wide EventBus instance. */
- public staticEventBusgetDefault() {if(defaultInstance ==null) {synchronized(EventBus.class) {if(defaultInstance ==null) {
- defaultInstance =newEventBus();
- }
- }
- }returndefaultInstance;
- }
EventBus 是单例模式存在的,使用了双重判断的方式,防止并发的问题,还能极大的提高效率。接着进入 register(this) 进行分析
- //**EventBus.class ---> register**
- /**
- * Registers the given subscriber to receive events. Subscribers must call {@link #unregister(Object)} once they
- * are no longer interested in receiving events.
- * <p/>
- * Subscribers have event handling methods that must be annotated by {@link Subscribe}.
- * The {@link Subscribe} annotation also allows configuration like {@link
- * ThreadMode} and priority.
- */
- public void register(Object subscriber) {//反射调用,获取订阅者的类对象Class subscriberClass = subscriber.getClass();//获取订阅者所有的订阅方法以@Subscribe为注解的一些public方法List subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
- //依次注册这些订阅方法
- synchronized(this) {for(SubscriberMethod subscriberMethod : subscriberMethods) {//对订阅方法进行注册subscribe(subscriber, subscriberMethod);
- }
- }
- }
其中有获取订阅方法的代码
- List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
我们进入,分析一下,如何获取订阅方法。首先来看一下订阅方法的类
- //**SubscriberMethod.class**
- /** Used internally by EventBus and generated subscriber indexes. */
- public class SubscriberMethod{
- finalMethod method;//方法
- finalThreadMode threadMode;//执行线程
- finalClass eventType;//接收的事件类型
- final intpriority;//优先级
- final booleansticky;//粘性事件
- /** Used for efficient comparison */String methodString;//...省略部分代码}
SubscriberMethod 是一个订阅方法的实体类,里面存储了订阅方法的一些基本信息,订阅方法就是在类中以 @Subscribe 为注解的一些 public 方法,注意是 public 方法否则会报错,为什么是 public 方法我们下面会分析,给出原因,然后进入 subscriberMethodFinder.findSubscriberMethods(subscriberClass),该代码的作用主要是获取当前类中所有的订阅方法。我们来看看是如何获取一个订阅者所有的订阅方法的:
- //**SubscriberMethodFinder.class**
- List findSubscriberMethods(ClasssubscriberClass) {
- //从缓存中获取订阅方法
- List subscriberMethods = METHOD_CACHE.get(subscriberClass);
- if(subscriberMethods !=null) {returnsubscriberMethods;
- }//是否忽略注解器生成的MyEventBusIndex类
- if(ignoreGeneratedIndex) {//利用反射来获取订阅类中的订阅方法信息subscriberMethods = findUsingReflection(subscriberClass);
- }else{//从注解器生成的MyEventBusIndex类中获得订阅类的订阅方法信息subscriberMethods = findUsingInfo(subscriberClass);
- }//当前类中没有订阅方法
- if(subscriberMethods.isEmpty()) {throw newEventBusException("Subscriber "+ subscriberClass
- +" and its super classes have no public methods with the @Subscribe annotation");
- }else{//保存进缓存METHOD_CACHE.put(subscriberClass, subscriberMethods);returnsubscriberMethods;
- }
- }
对于获取我们注册的订阅方法,首先就是通过缓存来获取,如果没有的话则通过以下两种方式进行获取:
对于这两种方式的分析,可以参考 http://www.jianshu.com/p/f057c460c77e 这里面相关的内容。
对于第一种方法没什么好说的,我们来分析一下通过反射来获取这些订阅方法的方式,接下来分析通过反射获取当前类中的订阅方法
- //**SubscriberMethodFinder.class**
- private List findUsingReflection(ClasssubscriberClass) {
- //FindState其实就是一个里面保存了订阅者和订阅方法信息的一个实体类,包括订阅类中所有订阅的事件类型和所有的订阅方法等。FindState findState = prepareFindState();
- findState.initForSubscriber(subscriberClass);while(findState.clazz !=null) {//获取订阅方法findUsingReflectionInSingleClass(findState);
- findState.moveToSuperclass();
- }returngetMethodsAndRelease(findState);
- }
代码不是太多,里面涉及到一个类 FindState,我们来看下,这是什么东西,
- static class FindState{
- //订阅方法
- finalList subscriberMethods = newArrayList<>();//以event为key,以method为value
- finalMap anyMethodByEventType = newHashMap<>();//以method的名字生成一个methodKey为key,该method的类(订阅者)为value
- finalMap subscriberClassByMethodKey = newHashMap<>();finalStringBuilder methodKeyBuilder =newStringBuilder(128);
- Class subscriberClass;
- Class clazz;booleanskipSuperClasses;
- SubscriberInfo subscriberInfo;//...省略部分代码}
这个 FindState 其实就是一个里面保存了订阅者和订阅方法信息的一个实体类,包括订阅类中所有订阅的事件类型和所有的订阅方法等。我们接着分析下面的代码。
- findUsingReflectionInSingleClass(findState)
这行代码便是获取订阅方法列表的重要代码,我们进入查看一下:
- //**SubscriberMethodFinder.class**
- private voidfindUsingReflectionInSingleClass(FindState findState) {//方法Method[] methods;try{//获取当前类中所有的方法
- // This is faster than getMethods, especially when subscribers are fat classes like Activitiesmethods = findState.clazz.getDeclaredMethods();
- }catch(Throwable th) {// Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149methods = findState.clazz.getMethods();
- findState.skipSuperClasses =true;
- }//遍历所有方法
- for(Method method : methods) {//获取方法的访问修饰权
- intmodifiers = method.getModifiers();//订阅方法必须是must be public, non-static, and non-abstract
- //获取订阅方法参数类型Class[] parameterTypes = method.getParameterTypes();//注解方法必须只有一个参数
- if(parameterTypes.length ==1) {//获取订阅方法的注解Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);//该订阅方法包含Subscribe注解
- if(subscribeAnnotation !=null) {
- Class eventType = parameterTypes[0];//添加该注解方法
- if(findState.checkAdd(method, eventType)) {//该注解方法的线程模式ThreadMode threadMode = subscribeAnnotation.threadMode();//添加该注解方法findState.subscriberMethods.add(newSubscriberMethod(method, eventType, threadMode,
- subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
- }
- }
- }else if(strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
- String methodName = method.getDeclaringClass().getName() +"."+ method.getName();throw newEventBusException("@Subscribe method "+ methodName +"must have exactly 1 parameter but has "+ parameterTypes.length);
- }
- }else if(strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
- String methodName = method.getDeclaringClass().getName() +"."+ method.getName();throw newEventBusException(methodName +" is a illegal @Subscribe method: must be public, non-static, and non-abstract");
- }
- }
- }
可以看到,首先会得到订阅类的 class 对象并通过反射获取订阅类中的所有方法信息,然后通过筛选获取到订阅方法集合。这里面就解释了为什么要以 @Subscribe 为注解的方法,且必须是 public 类型,方法参数只有一个的原因。
- //**Subscribe.java**
- @Documented@Retention(RetentionPolicy.RUNTIME)@Target({
- ElementType.METHOD
- }) public@interface Subscribe {
- ThreadMode threadMode()
- default ThreadMode.POSTING;
- /**
- * If true, delivers the most recent sticky event (posted with
- * {@link EventBus#postSticky(Object)}) to this subscriber (if event available).
- */
- boolean sticky()
- default false;
- /** Subscriber priority to influence the order of event delivery.
- * Within the same delivery thread ({@link ThreadMode}), higher priority subscribers will receive events before
- * others with a lower priority. The default priority is 0. Note: the priority does *NOT* affect the order of
- * delivery among subscribers with different {@link ThreadMode}s! */
- int priority()
- default 0;
- }
注解,分为三种参数,
ThreadMode,方法执行的线程,POSTING(默认值)、MAIN、BACKGROUND、ASYNC
sticky,粘性时间,默认值 false
priority,优先级,默认值 0
该方法流程是:
通过上面几步,我们就可以获得了所订阅的方法,然后分别进行注册这些订阅方法。通过下面的代码来执行:
- //参数:1订阅者 2订阅方法
- subscribe(subscriber, subscriberMethod);
接着分析这个注册方法。
- //**EventBus.java**
- // Must be called in synchronized block
- private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {//订阅方法的参数类型,也是事件类型Class eventType = subscriberMethod.eventType;//订阅方法描述,实体类(当前类中的订阅方法)Subscription newSubscription =newSubscription(subscriber, subscriberMethod);//获取当前类中的所有订阅方法CopyOnWriteArrayList subscriptions = subscriptionsByEventType.get(eventType);//该订阅方法还没有进行注册
- if(subscriptions ==null) {
- subscriptions =newCopyOnWriteArrayList<>();//注册该订阅方法subscriptionsByEventType.put(eventType, subscriptions);
- }else{//已经注册了,报异常
- if(subscriptions.contains(newSubscription)) {throw newEventBusException("Subscriber "+ subscriber.getClass() +" already registered to event "+ eventType);
- }
- }//总的所有的订阅方法
- intsize = subscriptions.size();for(inti =0; i <= size; i++) {//根据优先级,将订阅者插入到指定的位置
- if(i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
- subscriptions.add(i, newSubscription);break;
- }
- }//获取订阅者所有订阅的事件类型List> subscribedEvents = typesBySubscriber.get(subscriber);if(subscribedEvents ==null) {
- subscribedEvents =newArrayList<>();
- typesBySubscriber.put(subscriber, subscribedEvents);
- }//将该事件类型添加到typesBySubscriber中subscribedEvents.add(eventType);//如果接收sticky事件,立即分发sticky事件
- if(subscriberMethod.sticky) {if(eventInheritance) {// Existing sticky events of all subclasses of eventType have to be considered.
- // Note: Iterating over all events may be inefficient with lots of sticky events,
- // thus data structure should be changed to allow a more efficient lookup
- // (e.g. an additional map storing sub classes of super classes: Class -> List<Class>).Set, Object>> entries = stickyEvents.entrySet();for(Map.Entry, 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);
- }
- }
- }
这里面涉及到一些对象,我们分别注释一下:
- //Subscription.java
- //订阅者信息
- final class Subscription{
- finalObject subscriber;//订阅者
- finalSubscriberMethod subscriberMethod;//订阅方法}//subscriptionsByEventTypekey订阅方法类型 values 所有订阅了该类型的订阅者集合
- Map<Class,CopyOnWriteArrayList<Subscription>>subscriptionsByEventType;
- //typesBySubscriber
- key订阅者values订阅事件集合Map<Object,List<Class>>typesBySubscriber;
了解了这几个对象,上面的代码就很容易看懂了,
分析了注册事件后,我们来分析一下分发事件 post 的流程,首先通过
- EventBus.getDefault().post(new MessageEvent(""));
这行代码进行事件消息的分发,我们进入到 post 中详细了解一下这个流程。
- /** Posts the given event to the event bus. */
- 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 newEventBusException("Internal error. Abort state was not reset");
- }try{//分发事件
- while(!eventQueue.isEmpty()) {
- postSingleEvent(eventQueue.remove(0), postingState);
- }
- }finally{
- postingState.isPosting =false;
- postingState.isMainThread =false;
- }
- }
- }
代码虽然不长,但是不大好理解,这里面多了一些不常见的对象,我们来看下,首先对于第一行代码:
- PostingThreadState postingState = currentPostingThreadState.get();
这里面的 PostingThreadState 是什么意思呢?
- /** For ThreadLocal, much faster to set (and get multiple values). */
- final staticclass PostingThreadState {//当前线程的事件队列
- finalList eventQueue = newArrayList();
- //是否有事件正在分发
- booleanisPosting;//post的线程是否是主线程
- booleanisMainThread;
- Subscription subscription;
- Object event;booleancanceled;
- }
PostingThreadState 中包含了当前线程的事件队列,就是当前线程所有分发的事件都保存在 eventQueue 事件队列中以及订阅者订阅事件等信息,有了这些信息我们就可以从事件队列中取出事件分发给对应的订阅者。
我们接着分析,对于这个当前线程的事件队列,我们是通过
来得到的,对于这个 currentPostingThreadState 又是什么呢?
- currentPostingThreadState.get();
- private finalThreadLocal currentPostingThreadState = newThreadLocal() {
- @Override
- protectedPostingThreadStateinitialValue() {return newPostingThreadState();
- }
- };
ThreadLocal 是一个线程内部的数据存储类,通过它可以在指定的线程中存储数据,而这段数据是不会与其他线程共享的。可以看出 currentPostingThreadState 的实现是一个包含了 PostingThreadState 的 ThreadLocal 对象, 这样可以保证取到的都是自己线程对应的数据。
接着就通过
来对事件进行分发。
- postSingleEvent(eventQueue.remove(0), postingState);
- private void postSingleEvent(Objectevent, PostingThreadState postingState) throws Error {
- Class eventClass =event.getClass();
- boolean subscriptionFound =false;if(eventInheritance) {
- List> eventTypes = lookupAllEventTypes(eventClass);
- intcountTypes = eventTypes.size();for(inth =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(newNoSubscriberEvent(this,event));
- }
- }
- }
事件的分发最后还要通过
它来执行,
- postSingleEventForEventType
- privatebooleanpostSingleEventForEventType(Objectevent, 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;
- }//将事件分发给对应的订阅者
- private void postToSubscription(Subscription subscription, Objectevent, boolean isMainThread) {switch(subscription.subscriberMethod.threadMode) {casePOSTING:
- invokeSubscriber(subscription,event);break;caseMAIN:if(isMainThread) {
- invokeSubscriber(subscription,event);
- }else{
- mainThreadPoster.enqueue(subscription,event);
- }break;
来源: http://blog.csdn.net/liji_xc/article/details/70229742