EventBus3.1.1的特点和正确使用

1. 粘连事件Sticky

同样有这种粘连模式的还有boradcast, 记得RxJava也有的。

事件发出去了,可是接受端还没有生成实例。等到接收端生成后,马上能接受到事件,这就是粘连事件。

比如 activity A 发事件给activity B,但是activity B还没有生成。但是等到activity B生成,在onCreated()里调用register(this)后,就会触发订阅事件。基本上可以替代getIntent(), 尤其是解决intent过大会抛异常的问题.  但是要注意如果你在onresume里有代码。你要多方考虑,反正粘性事件实在register(this)里面执行的。

用完后记得根据需求删除粘连模式事件。

3.1.1版本的eventbus有bug, 导致不适合复杂的Case.

  •  发布粘性事件会触发所有同类型的订阅方法,即粘性和非粘性的事件都会触发。因为莫名其妙的多调用了post(event)
        public void postSticky(Object event) {
            synchronized (stickyEvents) {
                stickyEvents.put(event.getClass(), event);
            }
            //多此一举的post(), 既然是粘性事件,为何还要调用同类型的非粘性事件。
            // Should be posted after it is putted, in case the subscriber wants to remove immediately
            post(event);
        }
  • 粘性事件订阅方法无法第二次消费,这样就导致很难满足复杂项目的需求。
  • 多次调用和移除粘性事件时,post会执行多次粘性事件订阅方法(非粘性正常)

2. 优先级priority

订阅了两个一样事件,同时可以指定优先级。优先级高的先执行,优先级低的后执行。

@Subscribe(threadMode = ThreadMode.MAIN, priority = 1)
 public void onUpdate1Event(Events.EditDeviceNameEvent event) { } 

@Subscribe(threadMode = ThreadMode.MAIN, priority = 10) 
public void onUpdate2Event(Events.EditDeviceNameEvent event) { }

onUpdate2Event()会先被执行。onUpdate1Event后执行。曾经遇到的一个case: 是在其他activty手动删除Item,同时更新删除本activty的UI。 

 

3. 注解模式

注解模式是三个特点里最重要的一点,也是3.0更新的核心,因为性能得到了改善。但是由于默认是反射模式, 所以会搞错, 导致使用的依旧是反射模式。

使用注解模式必须添加如下代码和配置:

1. 引入注解处理器, 

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'
}

 2. 使用 index

配置好 annotationProcessor 后,还有区别的地方是 调用EventBus.getDefault().regisiter()之前,

必须在application的oncreate里面调用addIndex 方法,并且初始化EventBus。

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

 

Subscription 是一个方法的订阅信息,指明在哪个订阅类里的哪个订阅方法。

final class Subscription {
    final Object subscriber;
    final SubscriberMethod subscriberMethod;

}

 

注解模式的优点是,不再需要遍历订阅者类所有的方法,只需要遍历订阅的方法, 因为在编译阶段就处理好了订阅关系。

相当于这一步就不需要了:

List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);

public void register(Object subscriber) {
    Class<?> subscriberClass = subscriber.getClass();
    List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
    synchronized (this) {
        for (SubscriberMethod subscriberMethod : subscriberMethods) {
            subscribe(subscriber, subscriberMethod);
        }
    }
}

同时 缺点是订阅对象的生命周期过长。适当的时候可以调用clearCaches() , 会把订阅的所有信息清空

public static void clearCaches() {
    SubscriberMethodFinder.clearCaches();
    eventTypesCache.clear();
}

但之后使用eventbus,又要重新调用EventBus.builder().addIndex();

 

 


 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值