EventBus的配置&事件的优先级&使用索引

简介

由前面几篇我们学习了EventBus的使用、特性等等,这篇我们主要讲解的是我们如何在EventBus中如何自定义配置相关事项。还有设置事件的优先级案例分析。

EventBus-Android的发布 - 订阅

进入代码

事例说明

在EventBus的官方文档中也提到了EventBusBuilder类配置EventBus的各个方面事项。例如:以下是如何构建一个在发布的事件没有订阅者的情况下保持静态的EventBus:

EventBus eventBus = EventBus.builder()
    .logNoSubscriberMessages(false)
    .sendNoSubscriberEvent(false)
    .build();

另一个例子是当订阅者抛出异常时失败:

EventBus eventBus = EventBus.builder().throwSubscriberException(true).build();

配置默认的EventBus实例
使用EventBus.getDefault()是从应用程序中的任何位置获取共享EventBus实例的简单方法。EventBusBuilder还允许使用installDefaultEventBus ()方法配置此默认实例 。

例如,可以配置默认的EventBus实例以重新抛出订阅者方法中发生的异常。但是,仅限于DEBUG构建,因为这可能会使应用程序在例外情况下崩溃:

EventBus.builder().throwSubscriberException(BuildConfig.DEBUG).installDefaultEventBus();

如何使用

优先级

优先级priority使用

接下来我们来学习EventBus的优先级priority表示。priority来表示优先级,数值越大,优先级越高。在同一传递线程(ThreadMode)中,较高优先级的订户将在优先级较低的其他订户之前接收事件。默认优先级为0。

  • 注:优先级不会影响具有不同ThreadModes的订阅者的传递顺序!
@Subscribe(priority = 1);
public void onEvent(MessageEvent event) {
   //do something
}

取消事件传递

我们知道事件的优先级越高接收的数据最快,所以当优先级不想分发事件给低级别的事件时,可以使用cancelEventDelivery (Object event)这里的参数是订阅的实体参数。如下代码。

  • 注:当优先级更高的想取消事件传递时,只有当threadMode = ThreadMode.POSTING处于此状态才能取消事件传递有效。其他不行
@Subscribe(priority = 1000,threadMode = ThreadMode.POSTING)
public void onEvent(MessageEvent event){
     textView.setText(event.message);//设置接收的数据
    //取消事件传递,则低级别的事件无法接收到信息,只有在threadMode = ThreadMode.POSTING情况下
    EventBus.getDefault().cancelEventDelivery(event) ;
}

配置索引

订户索引是EventBus 3的新功能。它是一种可选的优化,可加快初始订户注册。可以使用EventBus批注处理器在构建期间创建订户索引。虽然不需要使用索引,但建议在Android上获得最佳性能。
当EventBus无法使用索引时,它将在运行时自动回退到反射。因此它仍然可以工作,只是有点慢。所以添加索引可以让EventBus的使用效率更高。

如何生成索引

使用annotationProcessor

如果您没有使用Android Gradle Plugin 2.2.0或更高版本,请使用android-apt配置。
要启用索引生成,您需要使用annotationProcessor 属性将EventBus批注处理器添加到构建中 。还要设置参数 eventBusIndex以指定要生成的索引的完全限定类。例如,将以下部分添加到app builde.radle构建脚本中:

android {
    defaultConfig {
        javaCompileOptions {
            annotationProcessorOptions {
                arguments = [ eventBusIndex : 'com.example.myapp.MyEventBusIndex' ] ##这里要修改为你项目的包名
            }
        }
    }
}

dependencies {
    implementation 'org.greenrobot:eventbus:3.1.1'
    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}

如果使用Kotlin,是用kapt

apply plugin: 'kotlin-kapt' // ensure kapt plugin is applied

dependencies {
    implementation 'org.greenrobot:eventbus:3.1.1'
    kapt 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}

kapt {
    arguments {
        arg('eventBusIndex', 'com.example.myapp.MyEventBusIndex')
    }
}

如果版本级别比较低则使用,(不推荐,应该升级了Gradle)

buildscript {
    dependencies {
        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.8'
    }
}

apply plugin: 'com.neenbedankt.android-apt'

dependencies {
    compile 'org.greenrobot:eventbus:3.1.1'
    apt 'org.greenrobot:eventbus-annotation-processor:3.1.1'
}

apt {
    arguments {
        eventBusIndex "com.example.myapp.MyEventBusIndex" #这里要修改为你项目的包名
    }
}

我的Module:app下build.gladle

apply plugin: 'com.android.application'


android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.eirunye.eventbus"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
        javaCompileOptions { # 添加的索引配置
            annotationProcessorOptions {
                arguments = [ eventBusIndex : 'com.eirunye.eventbus.MyEventBusIndex' ]  #这里要修改为你项目的包名
            }
        }
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    compileOptions {
        sourceCompatibility = '1.8'
        targetCompatibility = '1.8'
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
    # 添加的索引配置
    annotationProcessor 'org.greenrobot:eventbus-annotation-processor:3.1.1'
    implementation 'org.greenrobot:eventbus:3.1.1'
}

配置完成后重新Rebuild Project项目,然后我们到\app\build\generated\source\apt\debug\package\查看是否生成所配置的的文件MyEventBusIndex
MyEventBusIndex.png

package com.eirunye.eventbus;

import org.greenrobot.eventbus.meta.SimpleSubscriberInfo;
import org.greenrobot.eventbus.meta.SubscriberMethodInfo;
import org.greenrobot.eventbus.meta.SubscriberInfo;
import org.greenrobot.eventbus.meta.SubscriberInfoIndex;

import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

/** This class is generated by EventBus, do not edit. */
public class MyEventBusIndex implements SubscriberInfoIndex {
    private static final Map<Class<?>, SubscriberInfo> SUBSCRIBER_INDEX;

    static {
        SUBSCRIBER_INDEX = new HashMap<Class<?>, SubscriberInfo>();

        putIndex(new SimpleSubscriberInfo(PriorityTestActivity.class, true, new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("onEvent", com.eirunye.eventbus.bean.MessageEvent.class, ThreadMode.POSTING, 1000,
                    false),
            new SubscriberMethodInfo("onEventLow", com.eirunye.eventbus.bean.MessageEvent.class, ThreadMode.BACKGROUND,
                    1, false),
        }));

        putIndex(new SimpleSubscriberInfo(MainActivity.class, true, new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("onMessageEvent", com.eirunye.eventbus.bean.MessageEvent.class),
        }));

        putIndex(new SimpleSubscriberInfo(StickyTestActivity.class, true, new SubscriberMethodInfo[] {
            new SubscriberMethodInfo("onMessageStickyEvent", com.eirunye.eventbus.bean.MessageEvent.class,
                    ThreadMode.MAIN, 0, true),
        }));

    }

    private static void putIndex(SubscriberInfo info) {
        SUBSCRIBER_INDEX.put(info.getSubscriberClass(), info);
    }

    @Override
    public SubscriberInfo getSubscriberInfo(Class<?> subscriberClass) {
        SubscriberInfo info = SUBSCRIBER_INDEX.get(subscriberClass);
        if (info != null) {
            return info;
        } else {
            return null;
        }
    }
}

如何使用索引

我们可以在Application里面进行初始化。

EventBus eventBus = EventBus.builder().addIndex(new MyEventBusIndex()).build();

如下代码:

public class MyApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
    }
}

如果您想在整个应用中使用默认实例如下

EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();
// Now the default instance uses the given index. Use it like this:
EventBus eventBus = EventBus.getDefault();

您可以将相同的原则应用于作为库的一部分的代码(而不是最终的应用程序)。这样,您可以拥有多个索引类,您可以在EventBus设置期间添加这些索引类如下:

EventBus eventBus = EventBus.builder()
    .addIndex(new MyEventBusAppIndex())
    .addIndex(new MyEventBusLibIndex()).build();

注意:在第一次使用默认EventBus实例之前,这只能执行一次。对installDefaultEventBus()的后续调用 将引发异常。这可确保您的应用中的行为一致。所以应该在Application.class使用配置,和使用索引。

代码混淆

在开发中使用代码混淆的时候需要加上。

-keepattributes *Annotation*
-keepclassmembers class * {
    @org.greenrobot.eventbus.Subscribe <methods>;
}
-keep enum org.greenrobot.eventbus.ThreadMode { *; }
 
# Only required if you use AsyncExecutor
-keepclassmembers class * extends org.greenrobot.eventbus.util.ThrowableFailureEvent {
    <init>(java.lang.Throwable);
}

总结

EventBus的Configuration配置和索引使用的时候注意installDefaultEventBus()只能调用一次。否则报错。
事件的优先级中设置的值越大优先级越高,取消事件传递,必须是threadMode = ThreadMode.POSTING在这个情况下才能有效。
添加自动生成索引时的一些注意事项。

推荐

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章