美文网首页
EventBus原理解析

EventBus原理解析

作者: JustinJwu | 来源:发表于2018-05-22 17:05 被阅读0次

    EventBus(发布订阅事件总线):通过解耦发布者和订阅者简化android 事件传递

    EventBus is a publish/subscribe event bus for Android and Java.

    开源库地址:https://github.com/greenrobot/EventBus

    一、EventBus 简介

    1.简化了不同组件间(Activity,Fragment,Service) and 异步线程(Background Thread)与UI主线程间的通信

    2.传统的事件(消息)传递方式有:Intent、接口回调、Handler、BroadcastReceiver ,

    相比之下 EventBus 的优点:是代码简洁,使用简单,解耦事件的发布和订阅,更快,代码更小,50K 左右的 jar 包

    3.结构关系图:EventBus 负责存储订阅者、事件相关信息,订阅者和发布者都只和 EventBus 关联,实现了事件发布和订阅的解耦

    Events are posted ({@link #post(Object)}) to the bus, which delivers it to subscribers that have a matching handler method for the event type.

    4.事件响应流程图:

    订阅者首先调用 EventBus 的 register 接口订阅某种类型的事件,当发布者通过 post 接口发布该类型的事件时,EventBus 执行订阅者的事件响应函数。

    二、基本概念:

    1.事件(Event):其实就是一个Object对象,可以是String 类型,可以是一个自定义的Event对象,事件的分类:普通事件和黏性事件,EventType:表示事件所属的类

    2.发布者(Publisher):发布某事件的对象 ,post(Object),postSticky(Object)

    3.订阅者(Subscriber):订阅某种事件类型的对象,在事件响应函数中对事件进行处理

    Event handling methods must be annotated by {@link Subscribe}, must be public, return nothing (void), and have exactly one parameter (the event).

    三、如何使用

    How to get started with EventBus in 3 steps

    1.添加依赖库  compile'org.greenrobot:eventbus:3.1.1'

    2.定义事件类型

    3.声明订阅者事件响应方法:

    Event handling methods must be annotated by {@link Subscribe}, must be public, return nothing (void), and have exactly one parameter

    事件响应方法:必须:使用注解@Subscribe 声明 ,必须是public ,返回类型是void ,只有一个参数,Note that with EventBus 3 the method name can be chosen freely(EventBus3以后 方法名可以自定义)

    4.注册、解注册事件: In Android, in activities and fragments you should usually register according to their life cycle.

    Only while subscribers are registered, they will receive events.

    5.Post events: Post an event from any part of your code. All currently registered subscribers matching the event type will receive it. 所有与事件类型相匹配的当前已注册的订阅者都可以收到事件

    EventBus.getDefault().post(new MessageEvent("Hello everyone!"));

    四、源码解析:

    1. ThreadMode: http://greenrobot.org/eventbus/documentation/delivery-threads-threadmode/

    线程模型:ThreadMode类是枚举类,在订阅者事件响应方法中,可以通过注解的方式设置线程模型,EventBus内置了4中线程模型,分别是ThreadMode.POSTING 、ThreadMode.MAIN、ThreadMode.BACKGROUND、ThreadMode.ASYNC

    public enum ThreadMode {

        POSTING,(默认的模式)

        MAIN,

        MAIN_ORDERED,

        BACKGROUND,

        ASYNC

    }

    (1). POSTING:事件在哪个线程发布出来的,事件处理函数就会在这个线程中运行——收发在同一个线程(由于发布事件方法可能运行在主线程,这意味着接收方法中不能执行耗时操作,否则会阻塞主线程)

    (2).MAIN:不论事件是在哪个线程中发布出来的,该事件处理函数都会在UI线程中立即执行(这意味着接收事件方法不能执行耗时操作,否则会阻塞主线程;同时,由于是「立即」调用,所以发射事件方法此时是会被接收事件方法所阻塞的)

    (3).MAIN_ORDERED: 接收事件方法会被放入 MessageQueue 中等待执行(这意味着发射事件方法是不会被阻塞的)

    (4).BACKGROUND:如果事件是在UI线程中发布出来的,那么该事件处理函数就会在新的线程中运行,如果事件本来就是子线程中发布出来的,那么该事件处理函数直接在发布事件的线程中执行(不能执行UI更新操作)

    (5).ASYNC:无论事件在哪个线程发布,该事件处理函数都会在新建的子线程中执行(不能执行UI更新操作)

    2.黏性(Sticky )事件:当事件发布后,再有订阅者订阅该类型事件,依然能收到该类型事件最近一个 Sticky 事件。

    3.Subscribe

    线程模式,黏性事件,线程优先级

    4.EventBus

    register

    unRegister

    post

    5.EventBusBuilder

    参考文献:

    http://a.codekk.com/detail/Android/Trinea/EventBus%20%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90

    http://www.cnblogs.com/zhaoyanjun/p/6039221.html

    https://github.com/greenrobot/EventBus

    相关文章

      网友评论

          本文标题:EventBus原理解析

          本文链接:https://www.haomeiwen.com/subject/kyvpdftx.html