EventBus3.0简析

4 篇文章 0 订阅
1 篇文章 0 订阅

EventBus3.0


什么是EventBus

  EventBus是一个Android端优化的publish/subscribe消息总线,简化了应用程序内各组件间、组件与后台线程间的通信。比如请求网络,等网络返回时通过Handler或Broadcast通知UI,两个Fragment之间需要通过Listener通信,这些需求都可以通过EventBus实现。


EventBus框架

  大家谈到EventBus,总会想到greenrobot的EventBus,但是实际上EventBus是一个通用的叫法,例如Google出品的Guava,Guava是一个庞大的库,EventBus只是它附带的一个小功能,因此实际项目中使用并不多。用的最多的是greenrobot/EventBus,这个库的优点是接口简洁,集成方便,但是限定了方法名,不支持注解。另一个库square/otto修改自 Guava ,用的人也不少。

这篇博文暂时只讨论greenrobot的EventBus库。

基本用法

添加依赖库:
首先你要为你的app添加依赖库:

compile 'de.greenrobot:eventbus:3.0.0-beta1'

注册和反注册

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    EventBus.getDefault().register(this);
}

@Override
protected void onDestroy() {
    super.onDestroy();
    EventBus.getDefault().unregister(this);
}

发布者

  String str = “hello World!”
  EventBus.getDefault().post(str);

接收者

不同于2.4版本的四个方法,3.0版本使用注解完成事件接收。

@Subscribe(threadMode = ThreadMode.MainThread,priority = 100)
        //在ui线程执行,优先级为100
  public void helloEventBus(String message){
        //接收消息,处理消息
        String str = message;
    }

5.优先级高的可以拦截事件向下传递

 EventBus.getDefault().cancelEventDelivery(event) ;

原理初探

  你订阅了内容,所以你需要在该类注册EventBus,而你订阅的方法需要传入String,即你的接收信息为String类型,那么在post的时候,你post出去的也应该是String类型,其才会接收到消息。

3.0中发布粘性事件

EventBus.getDefault().postSticky(new VoteEvent(obj));
EventBus.getDefault().register(this);
@Subscribe(sticky = true)

  什么时候使用sticy,当你希望你的事件不被马上处理的时候,举个栗子,比如说,在一个详情页点赞之后,产生一个VoteEvent,VoteEvent并不立即被消费,而是等用户退出详情页回到商品列表之后,接收到该事件,然后刷新Adapter等。其实这就是之前我们用startActivityForResult和onActivityResult做的事情。

priority = 1

相信大部分人知道该用法,值越小优先级越低,默认为0。


建议

  使用EventBus时,创建一个时间类,将需要传递的参数封装成一个类,创建实例,进行传递。

public class Event  {  
    public static class UserListEvent {  
        public List<User> users ;  
    }

    public static class ItemListEvent {  
        public List<Item> items;  
    }    
}  

混淆处理

-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);
}

添加procesor

按照Markus Junginger的说法(EventBus创作者),在3.0中,如果你想进一步提升你的app的性能,你需要添加:
project的gradle中添加

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

app的 gradle添加

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

dependence{
compile 'de.greenrobot:eventbus:3.0.0-beta1'
provided 'de.greenrobot:eventbus-annotation-processor:3.0.0-beta1'
}


2.在dependencies的下方添加

apt {  
    arguments {  
        eventBusIndex "com.afei.test.MyEventBusIndex"  
        # 自定义的索引类
    }  
}  

通过rebuild即可自动生成索引类

添加索引到EventBus默认的单例中

EventBus.builder().addIndex(new MyEventBusIndex()).installDefaultEventBus();

这行代码做好在application的oncreate()中初始化,
其在编译的时候为注册类构建了一个索引,而不是在运行时,这样的结果是其让EventBus 3.0的性能提升了一倍,相比2.4来说,其会是它的3到6倍。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值