EventBus基本使用与源码解析

EventBus是一款针对Android优化的发布-订阅事件总线。它简化了应用程序内各组件间、组件与后台线程间的通信。其优点是开销小,代码更优雅,以及将发送者和接收者解耦。</span></span>当一个Android应用功能越来越多的时候,保证应用的各个部分之间高效的通信将变得越来越困难。所以为了解决这个问题,EventBus应运而生!</p> 

1.1 EventBus概述

        在讲到 EventBus 的基本用法之前,我们需要了解 EventBus 的三要素以及它的 4 种ThreadMode。

        EventBus的三要素如下。

        • Event:事件。可以是任意类型的对象。

        • Subscriber:事件订阅者。在 EventBus 3.0 之前消息处理的方法只能限定于 onEvent、onEventMainThread、onEventBackgroundThread和onEventAsync,它们分别代表4种线程模型。而在EventBus 3.0之后,事件处理的方法可以随便取名,但是需要添加一个注解@Subscribe,并且要指定线程模型(默认为POSTING)。 

        • Publisher:事件发布者。可以在任意线程任意位置发送事件,直接调用 EventBus 的post(Object)方法。可以自己实例化EventBus对象,但一般使用EventBus.getDefault()就可以。根据post函数参数的类型,会自动调用订阅相应类型事件的函数。

        EventBus的4种ThreadMode(线程模型)如下。

        • POSTING(默认):如果使用事件处理函数指定了线程模型为POSTING,那么该事件是在哪个线程发布出来的,事件处理函数就会在哪个线程中运行,也就是说发布事件和接收事件在同一个线程中。在线程模型为POSTING的事件处理函数中尽量避免执行耗时操作,因为它会阻塞事件的传递,甚至有可能会引起ANR。

        • MAIN:事件的处理会在UI线程中执行。事件处理的时间不能太长,长了会导致ANR。

        • BACKGROUND:如果事件是在UI线程中发布出来的,那么该事件处理函数就会在新的线程中运行;如果事件本来就是在子线程中发布出来的,那么该事件处理函数直接在发布事件的线程中执行。在此事件处理函数中禁止进行UI更新操作。

        • ASYNC:无论事件在哪个线程中发布,该事件处理函数都会在新建的子线程中执行;同样,此事件处理函数中禁止进行UI更新操作。

1.2 EventBus基本用法

        EventBus基本使用分为以下5个步骤。

      (1)自定义一个事件类


 
 
  1. public class MessageEvent {
  2. ......
  3. }

    (2)在需要订阅事件的地方注册事件

 EventBus.getDefault().register(this);
 
 

     (3)发送事件

EventBus.getDefault().post(messageEvent);
 
 

     (4)处理事件


 
 
  1. @Subscribe(threadMode = ThreadMode. MAIN)
  2. public void XXX( MessageEvent messageEvent){
  3. ...
  4. }

       前面说过,在EventBus3.0以后消息处理的方法可以随便取名,但是需要加一个注解@Subscribe,并且要指定线程模型(默认为POSTING)。

     (5)取消事件订阅

EventBus.getDefault().unregister(this);
 
 

1.3 EventBus实战

       前面讲到了EventBus的基本用法,但是这过于简单,这里举一个例子来应用EventBus。

     (1)添加依赖

implementation'org.greenrobot:eventbus:3.0.0'
 
 

     (2)添加混淆规则

       在模块的 proguard-rules.pro 混淆规则文件中添加如下规则:


 
 
  1. -keepattributes * Annotation*
  2. -keepclassmembers class * {
  3. @org.greenrobot.eventbus. Subscribe <methods>;
  4. }
  5. -keep enum org.greenrobot.eventbus. ThreadMode { *; }
  6. # And if you use AsyncExecutor:
  7. -keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
  8. <init>(java.lang. Throwable);
  9. }

      (3)定义消息事件类


 
 
  1. public class MessageEvent {
  2. public String message;
  3. public String getMessage( ) {
  4. return message;
  5. }
  6. public void setMessage( String message) {
  7. this. message = message;
  8. }
  9. }

     (4)注册和取消订阅事件

       在MainActivity中注册和取消订阅事件,在 MainActivity 中定义了两个 Button:一个用来注册事件,另一个用来跳转到SecondActivity。代码如下:


 
 
  1. public class MainActivity extends AppCompatActivity implements View. OnClickListener {
  2. private TextView tv_message;
  3. private Button btn_enter_second_activity, btn_register_event;
  4. @Override
  5. protected void onCreate( Bundle savedInstanceState) {
  6. super. onCreate(savedInstanceState);
  7. setContentView(R. layout. activity_main);
  8. tv_message = findViewById(R. id. tv_message);
  9. btn_enter_second_activity = findViewById(R. id. btn_enter_second_activity);
  10. btn_register_event = findViewById(R. id. btn_register_event);
  11. btn_enter_second_activity. setOnClickListener( this);
  12. btn_register_event. setOnClickListener( this);
  13. }
  14. @Override
  15. public void onClick( View v) {
  16. int id = v. getId();
  17. if (id == R. id. btn_enter_second_activity) {
  18. // 跳转到SecondActivity
  19. Intent intent = new Intent( this, SecondActivity. class);
  20. startActivity(intent);
  21. } else if (id == R. id. btn_register_event) {
  22. // 注册事件
  23. EventBus. getDefault(). register( this);
  24. }
  25. }
  26. @Override
  27. protected void onDestroy( ) {
  28. super. onDestroy();
  29. // 取消注册事件
  30. EventBus. getDefault(). unregister( this);
  31. }
  32. }

     (5)事件订阅者处理事件

       在MainActivity中自定义方法来处理事件,在这里ThreadMode设置为MAIN,事件的处理会在UI线程中执行,用TextView来展示收到的事件消息。


 
 
  1. @Subscribe(threadMode = ThreadMode.MAIN)
  2. public void onEvent(MessageEvent event) {
  3. if ( event != null && event.getMessage() != null) {
  4. tv_message.setText( event.getMessage());
  5. }
  6. }

      (6)事件发布者发布事件

        创建了SecondActivity来发布消息,在SecondActivity中,我们定义“发送事件 按钮来发送事件并将SecondActivity finish掉。代码如下所示:


 
 
  1. public class SecondActivity extends AppCompatActivity {
  2. private Button btn_send_event;
  3. @Override
  4. protected void onCreate( Bundle savedInstanceState) {
  5. super.onCreate(savedInstanceState);
  6. setContentView( R.layout.activity_second);
  7. btn_send_event = findViewById( R.id.btn_send_event);
  8. btn_send_event.setOnClickListener( new View. OnClickListener() {
  9. @Override
  10. public void onClick( View v) {
  11. MessageEvent event = new MessageEvent();
  12. event.setMessage( "欢迎学习EventBus");
  13. EventBus.getDefault().post(event);
  14. finish();
  15. }
  16. });
  17. }
  18. }

        好了,运行程序,如图1-3-1所示。接下来我们点击MainActivity 中的“注册事件 按钮来注册事件,然后点击“跳转到SecondActivity按钮,这时跳转到SecondActivity,如图1-3-2所示。接下来点击“发送事件 按钮,这个时候 SecondActivity 被 finish 掉,因此界面展示的是MainActivity,如图1-3-3所示。可以看到MainActivity的TextView显示“欢迎学习EventBus",MainActivity成功地收到了SecondActivity发送的事件。

图1-3-1 初始样式​​​​

 

图1-3-2 跳转到SecondActivity​​

 

图1-3-3 MainActivity接收到事件

 

1.4 EventBus的黏性事件

        除了上面讲的普通事件外, EventBus 还支持发送黏性事件,就是在发送事件之后再订阅该事件也能收
到该事件,这跟黏性广播类似。为了验证黏性事件,我们修改以前的代码,如下所示。
     ( 1 )订阅者处理黏性事件
       在 MainActivity 中新写一个方法用来处理黏性事件:

  
  
  1. @Subscribe(threadMode = ThreadMode.MAIN,sticky = true)
  2. public void onStickyEvent(MessageEvent event) {
  3. if ( event != null && event.getMessage() != null) {
  4. tv_message.setText( event.getMessage());
  5. }
  6. }
     ( 2 )发送黏性事件
 
       在 SecondActivity 中定义一个Button 来发送黏性事件:

   
   
  1. btn_send_event.setOnClickListener( new View.OnClickListener() {
  2. @Override
  3. public void onClick(View v) {
  4. MessageEvent event = new MessageEvent();
  5. event.setMessage( "EventBus黏性事件");
  6. EventBus.getDefault().postSticky( event);
  7. finish();
  8. }
  9. });
        现在运行代码再来看看效果。首先,我们在 MainActivity 中并没有点击“注册事件”按钮,而是直接跳到 SecondActivity中点击发送“发送事件”按钮。这时界面回到MainActivity,我们看到TextView仍旧显示着 MainActivity的字段,这是因为我们现在还没有订阅事件。接下来我们点击“注册事件”按钮,TextView 内容 发生改变,显示“EventBus黏性事件”,说明黏性事件被成功接收到了。
 

二 源码解析EventBus

2.1 EventBus构造方法

       当我们要使用 EventBus 时,首先会调用 EventBus.getDefault()来获取 EventBus 实例。现在查看 getDefault方法做了什么,如下所示:

    
    
  1. /** Convenience singleton for apps using a process-wide EventBus instance. */
  2. public static EventBus getDefault() {
  3. if (defaultInstance == null) {
  4. synchronized (EventBus. class) {
  5. if (defaultInstance == null) {
  6. defaultInstance = new EventBus();
  7. }
  8. }
  9. }
  10. return defaultInstance;
  11. }
       很明显这是一个单例模式,采用了双重检查模式 (DCL )。接下来查看 EventBus 的构造方法做了什 么
 

    
    
  1. /**
  2. * Creates a new EventBus instance; each instance is a separate scope in which events are delivered. To use a
  3. * central bus, consider {@link #getDefault()}.
  4. */
  5. public EventBus() {
  6. this(DEFAULT_BUILDER);
  7. }
这里 DEFAULT_BUILDER 是默认的 EventBusBuilder ,用来构造 EventBus :
private static final EventBusBuilder DEFAULT_BUILDER = new EventBusBuilder();
     
     

this调用了EventBus的另一个构造方法,如下所示:


    
    
  1. EventBus( EventBusBuilder builder) {
  2. // 以事件类型作为Key,Subscription的List集合作为Value的Map集合
  3. subscriptionsByEventType = new HashMap<>();
  4. // 订阅者作为Key,订阅事件的List集合作为Value的Map集合
  5. typesBySubscriber = new HashMap<>();
  6. // 黏性事件的Map集合
  7. stickyEvents = new ConcurrentHashMap<>();
  8. // Handler对象,用于线程间切换
  9. mainThreadPoster = new HandlerPoster( this, Looper.getMainLooper(), 10);
  10. // Runnable对象
  11. backgroundPoster = new BackgroundPoster( this);
  12. // Runnable对象
  13. asyncPoster = new AsyncPoster( this);
  14. indexCount = builder.subscriberInfoIndexes != null ? builder.subscriberInfoIndexes.size() : 0;
  15. subscriberMethodFinder = new SubscriberMethodFinder(builder.subscriberInfoIndexes,
  16. builder.strictMethodVerification, builder.ignoreGeneratedIndex);
  17. logSubscriberExceptions = builder.logSubscriberExceptions;
  18. logNoSubscriberMessages = builder.logNoSubscriberMessages;
  19. sendSubscriberExceptionEvent = builder.sendSubscriberExceptionEvent;
  20. sendNoSubscriberEvent = builder.sendNoSubscriberEvent;
  21. throwSubscriberException = builder.throwSubscriberException;
  22. eventInheritance = builder.eventInheritance;
  23. executorService = builder.executorService;
  24. }
       我们可以通过构造一个EventBusBuilder来对 EventBus 进行配置,这里采用了建造者模式。
       在这里注意到对于EventBus可以采用单实例模式获取,但是EventBus的构造方法为公共的。很显然也就是说明了在应用中可以存在多个EventBus,对于存在多个的EventBus情况下,它们之间相互独立,会发布和订阅各自的事件进行接收执行。

2.2 订阅者注册

       获取 EventBus 后,便可以将订阅者注册到 EventBus 中。下面来看一下register方法:
 

    
    
  1. /**
  2. * Registers the given subscriber to receive events. Subscribers must call {@link #unregister(Object)} once they
  3. * are no longer interested in receiving events.
  4. * <p/>
  5. * Subscribers have event handling methods that must be annotated by {@link Subscribe}.
  6. * The {@link Subscribe} annotation also allows configuration like {@link
  7. * ThreadMode} and priority.
  8. */
  9. public void register( Object subscriber) {
  10. Class<?> subscriberClass = subscriber. getClass();
  11. List< SubscriberMethod> subscriberMethods = subscriberMethodFinder. findSubscriberMethods(subscriberClass); // 1
  12. synchronized ( this) {
  13. for ( SubscriberMethod subscriberMethod : subscriberMethods) {
  14. subscribe(subscriber, subscriberMethod); // 2
  15. }
  16. }
  17. }
       对于register中的参数,就是我们的订阅者,也就是我们经常传入的this对象。
     ( 1 )查找订阅者的订阅方法
      上面代码注释 1 处的 findSubscriberMethods方法找出一个SubscriberMethod(对于SubscriberMethod类中,主要就是用保存订阅方法的Method对象,线程模式,事件类型,优先级,是否粘性事件等属性。 )的集合,也就是传进来的订阅 者的所有订阅方法,接下来遍历订阅者的订阅方法来完成订阅者的注册操作。可以看出register 方法做了两 件事:一件事是查找订阅者的订阅方法,另一件事是订阅者的注册。在SubscriberMethod 类中,主要用来保 存订阅方法的Method 对象、线程模式、事件类型、优先级、是否是黏性事件等属性。下面就来查看 findSubscriberMethods方法,如下所示:

    
    
  1. List< SubscriberMethod> findSubscriberMethods( Class<?> subscriberClass) {
  2. // 从缓存中获取SubscriberMethod集合
  3. List< SubscriberMethod> subscriberMethods = METHOD_CACHE. get(subscriberClass); // 1
  4. if (subscriberMethods != null) {
  5. return subscriberMethods;
  6. }
  7. //ignoreGeneratedIndex是否忽略注解器生成的MyEventBusIndex,默认为false
  8. if (ignoreGeneratedIndex) {
  9. //通过反射获取subscriberMethods
  10. subscriberMethods = findUsingReflection(subscriberClass);
  11. } else {
  12. //通过注解器生成的MyEventBusIndex信息获取subscriberMethods,
  13. //如果没有配置MyEventBusIndex,依然通过通过反射获取subscriberMethods
  14. subscriberMethods = findUsingInfo(subscriberClass); // 3
  15. }
  16. if (subscriberMethods. isEmpty()) {
  17. throw new EventBusException( "Subscriber " + subscriberClass
  18. + " and its super classes have no public methods with the @Subscribe annotation");
  19. } else {
  20. METHOD_CACHE. put(subscriberClass, subscriberMethods); // 2
  21. return subscriberMethods;
  22. }
  23. }
       上面代码注释 1 处从缓存中查找是否有订阅方法的集合,如果找到了就立马返回。如果缓存中没有,则 根据 ignoreGeneratedIndex 属性的值来选择采用何种方法来查找订阅方法的集合。 ignoreGeneratedIndex 属性 表示是否忽略注解器生成的 MyEventBusIndex(在项目重新rebuild以后,会自动生成在build文件夹下,类名也可以自己定义) 。如何生成 MyEventBusIndex 类以及它的使用,可以参考官方 文档 http : //greenrobot.org/eventbus/documentation/subscriber-index/,这里就不再讲解了。 ignoreGeneratedIndex 的默认值是 false ,可以通过 EventBusBuilder 来设置它的值。在注释 2 处找到订阅方法的 集合后,放入缓存,以免下次继续查找。我们在项目中经常通过EventBus 单例模式来获取默认的 EventBus 对 象,也就是ignoreGeneratedIndex 为 false 的情况,这种情况调用了注释 3 处的 findUsingInfo 方法:
 

    
    
  1. private List<SubscriberMethod> findUsingInfo( Class<?> subscriberClass) {
  2. //创建和初始化FindState对象
  3. FindState findState = prepareFindState();
  4. findState.initForSubscriber(subscriberClass);
  5. while (findState.clazz != null) {
  6. //获取订阅者信息,没有配置MyEventBusIndex返回null
  7. findState.subscriberInfo = getSubscriberInfo(findState); // 1
  8. if (findState.subscriberInfo != null) {
  9. SubscriberMethod[] array = findState.subscriberInfo.getSubscriberMethods(); // 2
  10. for (SubscriberMethod subscriberMethod : array) {
  11. if (findState.checkAdd(subscriberMethod.method, subscriberMethod.eventType)) {
  12. findState.subscriberMethods.add(subscriberMethod);
  13. }
  14. }
  15. } else {
  16. //通过反射来查找订阅方法
  17. findUsingReflectionInSingleClass(findState); // 3
  18. }
  19. //进入父类查找订阅方法
  20. findState.moveToSuperclass();
  21. }
  22. //回收处理findState,并返回订阅方法的List集合
  23. return getMethodsAndRelease(findState);
  24. }
      在FindState里面,它保存了一些订阅者的方法以及对订阅方法的校验。通过initForSubscriber初始化了FindState的clazz属性。  上面代码注释 1 处通过 getSubscriberInfo 方法来获取订阅者信息。在我们开始查找订阅方法的时候并没有忽略注解器为我们生成的索引 MyEventBusIndex 。如果我们通过 EventBusBuilder 配置了 MyEventBusIndex,便会获取 subscriberInfo 。注释 2 处调用 subscriberInfo 的 getSubscriberMethods 方法便可以得 到订阅方法相关的信息。如果没有配置MyEventBusIndex ,便会执行注释 3 处的 findUsingReflectionInSingleClass方法,将订阅方法保存到 findState 中。最后再通过 getMethodsAndRelease 方法findState 做回收处理并返回订阅方法的 List 集合。默认情况下是没有配置 MyEventBusIndex 的,因此现在 查看一下findUsingReflectionInSingleClass方法的执行过程,如下所示:
 
 

    
    
  1. private void findUsingReflectionInSingleClass (FindState findState) {
  2. Method[] methods;
  3. try {
  4. // This is faster than getMethods, especially when subscribers are fat classes like Activities
  5. methods = findState.clazz.getDeclaredMethods(); // 1
  6. } catch (Throwable th) {
  7. // Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149
  8. methods = findState.clazz.getMethods();
  9. findState.skipSuperClasses = true;
  10. }
  11. for (Method method : methods) {
  12. //对订阅方法的类型进行过滤
  13. int modifiers = method.getModifiers();
  14. if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
  15. Class<?>[] parameterTypes = method.getParameterTypes();
  16. //定于方法中只能有一个参数
  17. if (parameterTypes.length == 1) {
  18. //查找包含Subscribe的注解
  19. Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
  20. if (subscribeAnnotation != null) {
  21. //保存到findState对象当中
  22. Class<?> eventType = parameterTypes[ 0];
  23. if (findState.checkAdd(method, eventType)) {
  24. ThreadMode threadMode = subscribeAnnotation.threadMode();
  25. findState.subscriberMethods.add( new SubscriberMethod(method, eventType, threadMode,
  26. subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
  27. }
  28. }
  29. } else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
  30. String methodName = method.getDeclaringClass().getName() + "." + method.getName();
  31. throw new EventBusException( "@Subscribe method " + methodName +
  32. "must have exactly 1 parameter but has " + parameterTypes.length);
  33. }
  34. } else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
  35. String methodName = method.getDeclaringClass().getName() + "." + method.getName();
  36. throw new EventBusException(methodName +
  37. " is a illegal @Subscribe method: must be public, non-static, and non-abstract");
  38. }
  39. }
  40. }
       上面代码注释 1 处通过反射来获取订阅者中所有的方法,并根据方法的类型、参数和注解来找到订阅方
法。找到订阅方法后将订阅方法的相关信息保存到 findState 中。
 
     ( 2 )订阅者的注册过程
       在查找完订阅者的订阅方法以后便开始对所有的订阅方法进行注册。我们再回到 register方法中,在那里的注释 2 处调用了 subscribe 方法来对订阅方法进行注册,如下所示:
 

    
    
  1. // Must be called in synchronized block
  2. private void subscribe( Object subscriber, SubscriberMethod subscriberMethod) {
  3. // 获取订阅方法中的订阅事件
  4. Class<?> eventType = subscriberMethod. eventType;
  5. // 创建一个SubScription来保存订阅者和订阅方法
  6. Subscription newSubscription = new Subscription(subscriber, subscriberMethod); // 1
  7. //获取当前订阅事件中Subscription的List集合
  8. CopyOnWriteArrayList< Subscription> subscriptions = subscriptionsByEventType. get(eventType); // 2
  9. if (subscriptions == null) {
  10. //该事件对应的Subscription的List集合不存在,则重新创建并保存在subscriptionsByEventType中
  11. subscriptions = new CopyOnWriteArrayList<>();
  12. subscriptionsByEventType. put(eventType, subscriptions);
  13. } else {
  14. // 判断订阅者是否已经被注册
  15. if (subscriptions. contains(newSubscription)) {
  16. throw new EventBusException( "Subscriber " + subscriber. getClass() + " already registered to event "
  17. + eventType);
  18. }
  19. }
  20. //将newSubscription按照订阅方法的优先级插入到subscriptions中
  21. int size = subscriptions. size();
  22. for (int i = 0; i <= size; i++) {
  23. if (i == size || subscriberMethod. priority > subscriptions. get(i). subscriberMethod. priority) {
  24. subscriptions. add(i, newSubscription); // 3
  25. break;
  26. }
  27. }
  28. //通过订阅者获取该订阅者所订阅事件的集合
  29. List< Class<?>> subscribedEvents = typesBySubscriber. get(subscriber); // 4
  30. if (subscribedEvents == null) {
  31. subscribedEvents = new ArrayList<>();
  32. typesBySubscriber. put(subscriber, subscribedEvents);
  33. }
  34. //将当前的订阅事件添加到subscribedEvents中
  35. subscribedEvents. add(eventType);
  36. // 黏性事件的处理
  37. if (subscriberMethod. sticky) {
  38. if (eventInheritance) {
  39. // Existing sticky events of all subclasses of eventType have to be considered.
  40. // Note: Iterating over all events may be inefficient with lots of sticky events,
  41. // thus data structure should be changed to allow a more efficient lookup
  42. // (e.g. an additional map storing sub classes of super classes: Class -> List<Class>).
  43. Set< Map. Entry< Class<?>, Object>> entries = stickyEvents. entrySet();
  44. for ( Map. Entry< Class<?>, Object> entry : entries) {
  45. Class<?> candidateEventType = entry. getKey();
  46. if (eventType. isAssignableFrom(candidateEventType)) {
  47. Object stickyEvent = entry. getValue();
  48. checkPostStickyEventToSubscription(newSubscription, stickyEvent);
  49. }
  50. }
  51. } else {
  52. Object stickyEvent = stickyEvents. get(eventType);
  53. checkPostStickyEventToSubscription(newSubscription, stickyEvent);
  54. }
  55. }
  56. }
       首先,上面代码注释 1 处会根据 subscriber (订阅者)和 subscriberMethod (订阅方法)创建一个 Subscription(订阅对象)。注释 2 处根据 eventType (事件类型)获取 Subscriptions (订阅对象集合)。如果 Subscriptions为 null 则重新创建,并将 Subscriptions 根据 eventType 保存在 subscriptionsByEventType ( Map 集 合)。注释3 处按照订阅方法的优先级插入到订阅对象集合中,完成订阅方法的注册。注释 4 处通过 subscriber获取 subscribedEvents (事件类型集合)。如果 subscribedEvents 为 null 则重新创建,并将 eventType 添加到subscribedEvents 中,并根据 subscriber 将 subscribedEvents 存储在 typesBySubscriber ( Map 集合)。如果 是黏性事件,则从stickyEvents 事件保存队列中取出该事件类型的事件发送给当前订阅者。总结一下, subscribe方法主要就是做了两件事:一件事是将Subscriptions 根据 eventType 封装到 subscriptionsByEventType 中,将subscribedEvents 根据 subscriber 封装到 typesBySubscriber 中;第二件事就是对黏性事件的处理。

 

2.3 事件的发送

       在获取 EventBus 对象以后,可以通过 post 方法来进行对事件的提交。post方法的源码如下所示:

     
     
  1. /** Posts the given event to the event bus. */
  2. public void post(Object event) {
  3. // PostingThreadState 保存着事件队列和线程状态信息
  4. PostingThreadState postingState = currentPostingThreadState. get();
  5. // 获取事件队列,并将当前事件插入事件队列
  6. List<Object> eventQueue = postingState.eventQueue;
  7. eventQueue. add( event);
  8. if (!postingState.isPosting) {
  9. postingState.isMainThread = Looper.getMainLooper() == Looper.myLooper();
  10. postingState.isPosting = true;
  11. if (postingState.canceled) {
  12. throw new EventBusException( "Internal error. Abort state was not reset");
  13. }
  14. try {
  15. // 处理队列中的所有事件
  16. while (!eventQueue.isEmpty()) {
  17. postSingleEvent(eventQueue. remove( 0), postingState);
  18. }
  19. } finally {
  20. postingState.isPosting = false;
  21. postingState.isMainThread = false;
  22. }
  23. }
  24. }
       首先从 PostingThreadState 对象中取出事件队列,然后再将当前的事件插入事件队列。最后将队列中的 事件依次交由 postSingleEvent 方法进行处理,并移除该事件。之后查看 postSingleEvent 方法里做了什么:

      
      
  1. private void postSingleEvent(Object event, PostingThreadState postingState) throws Error {
  2. Class<?> eventClass = event.getClass();
  3. boolean subscriptionFound = false;
  4. // eventInheritance 表示是否向上查找事件的父类,默认为true
  5. if (eventInheritance) {
  6. //获取所有事件并存放在List中,这里表示事件存在继承关系,向上查找事件的父类
  7. List<Class<?>> eventTypes = lookupAllEventTypes(eventClass);
  8. int countTypes = eventTypes.size();
  9. for ( int h = 0; h < countTypes; h++) {
  10. Class<?> clazz = eventTypes. get(h);
  11. subscriptionFound |= postSingleEventForEventType( event, postingState, clazz);
  12. }
  13. } else {
  14. subscriptionFound = postSingleEventForEventType( event, postingState, eventClass);
  15. }
  16. // 找不到该事件时的异常处理
  17. if (!subscriptionFound) {
  18. if (logNoSubscriberMessages) {
  19. Log.d(TAG, "No subscribers registered for event " + eventClass);
  20. }
  21. if (sendNoSubscriberEvent && eventClass != NoSubscriberEvent. class &&
  22. eventClass != SubscriberExceptionEvent. class) {
  23. post( new NoSubscriberEvent( this, event));
  24. }
  25. }
  26. }
       eventInheritance 表示是否向上查找事件的父类,它的默认值为 true ,可以通过在 EventBusBuilder 中进行 配置。当eventInheritance 为 true 时,则通过 lookupAllEventTypes 找到所有的父类事件并存在 List 中,然后通过postSingleEventForEventType方法对事件逐一处理。postSingleEventForEventType方法的源码如下所示:
 

      
      
  1. private boolean postSingleEventForEventType(Object event, PostingThreadState postingState, Class<?> eventClass) {
  2. CopyOnWriteArrayList<Subscription> subscriptions;
  3. synchronized ( this) {
  4. subscriptions = subscriptionsByEventType. get(eventClass); // 1
  5. }
  6. if (subscriptions != null && !subscriptions.isEmpty()) {
  7. for (Subscription subscription : subscriptions) { // 2
  8. postingState. event = event;
  9. postingState.subscription = subscription;
  10. boolean aborted = false;
  11. try {
  12. postToSubscription(subscription, event, postingState.isMainThread);
  13. aborted = postingState.canceled;
  14. } finally {
  15. postingState. event = null;
  16. postingState.subscription = null;
  17. postingState.canceled = false;
  18. }
  19. if (aborted) {
  20. break;
  21. }
  22. }
  23. return true;
  24. }
  25. return false;
  26. }
       上面代码注释 1 处同步取出该事件对应的 Subscriptions (订阅对象集合)。注释 2 处遍历 Subscriptions , 将事件 event 和对应的 Subscription (订阅对象)传递给 postingState 并调用 postToSubscription 方法对事件进 行处理。接下来查看postToSubscription 方法:

       
       
  1. private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
  2. switch (subscription.subscriberMethod.threadMode) {
  3. case POSTING:
  4. invokeSubscriber(subscription, event);
  5. break;
  6. case MAIN:
  7. if (isMainThread) {
  8. invokeSubscriber(subscription, event);
  9. } else {
  10. mainThreadPoster.enqueue(subscription, event);
  11. }
  12. break;
  13. case BACKGROUND:
  14. if (isMainThread) {
  15. backgroundPoster.enqueue(subscription, event);
  16. } else {
  17. invokeSubscriber(subscription, event);
  18. }
  19. break;
  20. case ASYNC:
  21. asyncPoster.enqueue(subscription, event);
  22. break;
  23. default:
  24. throw new IllegalStateException( "Unknown thread mode: " + subscription.subscriberMethod.threadMode);
  25. }
  26. }
       取出订阅方法的 threadMode (线程模式),之后根据 threadMode 来分别处理。如果 threadMode 是 MAIN,若提交事件的线程是主线程,则通过反射直接运行订阅的方法;若其不是主线程,则需要 mainThreadPoster 将我们的订阅事件添加到主线程队列中。 mainThreadPoster 是 HandlerPoster 类型的,继承 自Handler ,通过 Handler 将订阅方法切换到主线程执行。
 

2.4 订阅者取消注册

       取消注册则需要调用unregister方法,如下所示:


      
      
  1. /** Unregisters the given subscriber from all event classes. */
  2. public synchronized void unregister(Object subscriber) {
  3. List<Class<?>> subscribedTypes = typesBySubscriber. get(subscriber); // 1
  4. if (subscribedTypes != null) {
  5. for (Class<?> eventType : subscribedTypes) {
  6. unsubscribeByEventType(subscriber, eventType); // 2
  7. }
  8. typesBySubscriber. remove(subscriber); // 3
  9. } else {
  10. Log.w(TAG, "Subscriber to unregister was not registered before: " + subscriber.getClass());
  11. }
  12. }
       我们在订阅者注册的过程中讲到过 typesBySubscriber ,它是一个map 集合。上面代码注释 1 处通过 subscriber找到 subscribedTypes (事件类型集合)。注释 3 处将 subscriber 对应的 eventType 从 typesBySubscriber 中移除。注释2 处遍历 subscribedTypes ,并调用 unsubscribeByEventType 方法:

       
       
  1. /** Only updates subscriptionsByEventType, not typesBySubscriber! Caller must update typesBySubscriber. */
  2. private void unsubscribeByEventType(Object subscriber, Class<?> eventType) {
  3. List<Subscription> subscriptions = subscriptionsByEventType. get(eventType); // 1
  4. if (subscriptions != null) {
  5. int size = subscriptions.size();
  6. for ( int i = 0; i < size; i++) {
  7. Subscription subscription = subscriptions. get(i);
  8. if (subscription.subscriber == subscriber) {
  9. subscription.active = false;
  10. subscriptions. remove(i);
  11. i--;
  12. size--;
  13. }
  14. }
  15. }
  16. }
       上面代码注释 1 处通过 eventType 来得到对应的 Subscriptions (订阅对象集合),并在 for 循环中判断如果 Subscription (订阅对象)的 subscriber (订阅者)属性等于传进来的 subscriber ,则从 Subscriptions 中移除该 Subscription。
 

总结

  从整个EventBus的执行过程来它,他实际上就是一个典型的观察者模式。通过对事件的发布与订阅,实现了一种一对多的依赖关系,并有效的为我们事件的发送者与接收者之间进行了解耦。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值