What‘s EventBus?
EventBus是一个开源用于事件的发布和订阅的框架。
官方网站:http://greenrobot.org/eventbus/
Git地址:https://github.com/greenrobot/EventBus
EventBus is publish/subscribe event bus optimized for Android.
simplifies the communication between components
- decouples event senders and receivers
- performs well with Activities, Fragments, and background threads
- avoids complex and error-prone dependencies and life cycle issues
EventBus简化了应用程序内各组件间、组件与后台线程间的通信。比如请求网络,等网络返回时通过Handler或Broadcast通知UI,两个Fragment之间需要通过Listener通信,这些需求都可以通过EventBus实现。
EventBus的简单使用
Android开发常见的场景:UI主线程开启一个子线程执行耗时操作,子线程执行完毕通过handler发送message到主线程,主线程通过handleMessage(msg)进行后续的处理。
UI主线程与子线程间的通信通常使用Handler的消息队列,而使用EventBus则可转换为如下:
public class MainActivity extends AppCompatActivity {
public class MSGEvent {
final String msg;
public MSGEvent() {
msg = "Hello message";
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
EventBus.getDefault().register(this);
new Thread(){
@Override
public void run() {
try {
sleep(2000);
EventBus.getDefault().post(new MSGEvent());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
@Override
protected void onDestroy() {
super.onDestroy();
EventBus.getDefault().unregister(this);
}
@Subscribe(threadMode = ThreadMode.MAIN)
public void onMessageEvent(MSGEvent event) {
Toast.makeText(this, event.msg, Toast.LENGTH_SHORT).show();
}
}
订阅和取消订阅
EventBus.getDefault().register(this); EventBus.getDefault().unregister(this);
跟Android的broadcast类似,需要注册事件监听才能收到订阅,一般在oncreate/onstart注册事件订阅;ondestroy/onstop取消注册。
定义事件
所谓事件,就是可以理解为在通信双方间传递的对象;在broadcast当中,发送的是Intent对象,handler消息队列里面使用的时Message对象。
而EventBus的事件可以为任意Object,即可随你自定义一个对象即可当做一个事件类型,如示例中的MSGEvent定义事件的接收处理
事件的最终接收并处理的地方,即Handler机制的handleMessage(msg),Broadcast机制的 onReceive(Context context, Intent intent)函数;
EventBus内通过注解@Subscribe来声明该函数为一个事件接收处理函数,参数则为方才定义好的事件类型。线程间通信时,可选参数控制处理函数在何种线程执行,如上述例子:@Subscribe(threadMode = ThreadMode.MAIN),标识接收处理函数需要在主线程执行。
P.S onMessageEvent(MSGEvent event) 事件接收处理函数需要为public void,参数为仅能有一个为需要接收的对应的事件对象。发送事件
EventBus.getDefault().post(new MSGEvent());
将需要发送的事件,通过EventBus.getDefault().post函数即可发送,类似handler机制的sendMessage(sendMessage)。
EventBus原理简要分析
参考了博文:http://www.cnblogs.com/angeldevil/p/3715934.html
上述博文基于较为旧版的EventBus库做了一番分析,这里以其为基础简要的介绍下其实现原理。
首先需要了解源码中以下几个对象以及变量名的意义:
Class<\?> eventType; 源码中这个名为eventType的java Class<\?>类对象,代表了上文中的定义的事件即示例中的MSGEvent class.
订阅对象类—-Subscription包含了:
- 订阅源subscriber订阅源即为EventBus.getDefault().register(Object subscriber)的参数subscriber,如上文示例中的MainActivity对象
- SubscribMethod暂可简要理解为上文中的事件的接收处理函数的描述类,后文将有具体分析
final class Subscription {
final Object subscriber; //订阅源
final SubscriberMethod subscriberMethod;
...................
.............
Subscription(Object subscriber, SubscriberMethod subscriberMethod) {
this.subscriber = subscriber;
this.subscriberMethod = subscriberMethod;
active = true;
}
...........
................
}
EventBus.java为其核心实现类,其主要持有维护了下面几个重要的Map数据:
/*EventType -> List<Subscription>,事件到订阅对象之间的映射*/
private final Map<Class<?>, CopyOnWriteArrayList<Subscription>> subscriptionsByEventType;
// Subscriber -> List<EventType>,订阅源到它订阅的的所有事件类型的映射
private final Map<Object, List<Class<?>>> typesBySubscriber;
EventBus源码核心流程
基于eventbus 3.0.0版本
入口EventBus.getDefault().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();
List<SubscriberMethod> subscriberMethods = subscriberMethodFinder.findSubscriberMethods(subscriberClass);
synchronized (this) {
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
}
}
register函数的参数即为订阅源subscriber一个Object对象,而后执行的findSubscriberMethods(subscriberClass)即通过注解@Subscribe查找并获得订阅源类对象subscriberClass以及其父类内所有的事件接收处理函数,函数最终会调用到如下函数:
private void findUsingReflectionInSingleClass(FindState findState) {
Method[] methods;
try {
// This is faster than getMethods, especially when subscribers are fat classes like Activities
methods = findState.clazz.getDeclaredMethods();
} catch (Throwable th) {
// Workaround for java.lang.NoClassDefFoundError, see https://github.com/greenrobot/EventBus/issues/149
methods = findState.clazz.getMethods();
findState.skipSuperClasses = true;
}
for (Method method : methods) {
int modifiers = method.getModifiers();
//这里限定了接收处理函数需要为public
if ((modifiers & Modifier.PUBLIC) != 0 && (modifiers & MODIFIERS_IGNORE) == 0) {
Class<?>[] parameterTypes = method.getParameterTypes();
//限定了接收处理函数仅能拥有一个参数
if (parameterTypes.length == 1) {
//函数需要有@Subscribe注解
Subscribe subscribeAnnotation = method.getAnnotation(Subscribe.class);
if (subscribeAnnotation != null) {
//事件接收处理函数的唯一一个参数即为eventType
Class<?> eventType = parameterTypes[0];
if (findState.checkAdd(method, eventType)) {
ThreadMode threadMode = subscribeAnnotation.threadMode();
findState.subscriberMethods.add(new SubscriberMethod(method, eventType, threadMode,
subscribeAnnotation.priority(), subscribeAnnotation.sticky()));
}
}
} else if (strictMethodVerification && method.isAnnotationPresent(Subscribe.class)) {
String methodName = method.getDeclaringClass().getName() + "." + method.getName();
throw new EventBusException("@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 new EventBusException(methodName +
" is a illegal @Subscribe method: must be public, non-static, and non-abstract");
}
}
}
此过程生成subscriberClass内所有的带@Subscribe注解并且仅有一个函数参数的事件接收处理函数
new SubscriberMethod(method, eventType, threadMode,subscribeAnnotation.priority(), subscribeAnnotation.sticky())
后续将遍历所有这些获得的List,然后将eventType,SubscriberMethod,subscriber之间的映射关系以Map的形式存储在上述的Map数据内;至此EventBus即可通过Subscriber到EventType的映射,我们就可以很方便地使一个Subscriber取消接收事件,通过EventType到Sucscribtion的映射,可以方便地将相应的事件发送到它的每一个订阅者。
for (SubscriberMethod subscriberMethod : subscriberMethods) {
subscribe(subscriber, subscriberMethod);
}
// Must be called in synchronized block
private void subscribe(Object subscriber, SubscriberMethod subscriberMethod) {
Class<?> eventType = subscriberMethod.eventType;
Subscription newSubscription = new Subscription(subscriber, subscriberMethod);
CopyOnWriteArrayList<Subscription> subscriptions = subscriptionsByEventType.get(eventType);
if (subscriptions == null) {
subscriptions = new CopyOnWriteArrayList<>();
subscriptionsByEventType.put(eventType, subscriptions);
} else {
if (subscriptions.contains(newSubscription)) {
throw new EventBusException("Subscriber " + subscriber.getClass() + " already registered to event "
+ eventType);
}
}
int size = subscriptions.size();
for (int i = 0; i <= size; i++) {
if (i == size || subscriberMethod.priority > subscriptions.get(i).subscriberMethod.priority) {
subscriptions.add(i, newSubscription);
break;
}
}
List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
if (subscribedEvents == null) {
subscribedEvents = new ArrayList<>();
typesBySubscriber.put(subscriber, subscribedEvents);
}
subscribedEvents.add(eventType);
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<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
for (Map.Entry<Class<?>, 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);
}
}
}