前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Eventbus3代码分析(四):@interface Subscribe分析

Eventbus3代码分析(四):@interface Subscribe分析

作者头像
dodo_lihao
发布2018-09-12 10:50:22
4660
发布2018-09-12 10:50:22
举报
文章被收录于专栏:懒人开发懒人开发

@interface Subscribe分析

前面参考的代码,存放在(use_little_demo中的 eventbus3testhttps://github.com/2954722256/use_little_demo

我们通过eventbus3的注解

用ctrl+鼠标左键, 跟进去,可以看见 @interface Subscribe

代码语言:javascript
复制
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface Subscribe {
    ThreadMode threadMode() default ThreadMode.POSTING;

    /**
     * If true, delivers the most recent sticky event (posted with
     * {@link EventBus#postSticky(Object)}) to this subscriber (if event available).
     */
    boolean sticky() default false;

    /** Subscriber priority to influence the order of event delivery.
     * Within the same delivery thread ({@link ThreadMode}), higher priority subscribers will receive events before
     * others with a lower priority. The default priority is 0. Note: the priority does *NOT* affect the order of
     * delivery among subscribers with different {@link ThreadMode}s! */
    int priority() default 0;
}

我们可以看见,和我们自己例子类似的结构 这里多了个@Documentd, 这个也提到过,文档相关的,我们可以忽略

简单扯淡 通过

  • @Retention(RetentionPolicy.RUNTIME)
  • @Target({ElementType.METHOD})

我们可以知道,是放在方法上面使用的 (我们上一篇是放在类上面使用的,和这个不同的是,只是放在方法前面使用) 也就是上图使用的形式

这里接口,有3个方法 我们可以发现,对应的一些配置,例如

其实,都是通过注解传入值的 (上一篇,我们也有对应的传值例子,具体可以参考)


简单复习

第一篇,我们提到过,有4中模式 也就是这里配置的threadMode()返回类型ThreadMode


我们来看一下ThreadMode类 ThreadMode

代码语言:javascript
复制
/**
 * Each event handler method has a thread mode, which determines in which thread the method is to be called by EventBus.
 * EventBus takes care of threading independently from the posting thread.
 * 
 * @see EventBus#register(Object)
 * @author Markus
 */
public enum ThreadMode {
    /**
     * Subscriber will be called in the same thread, which is posting the event. This is the default. Event delivery
     * implies the least overhead because it avoids thread switching completely. Thus this is the recommended mode for
     * simple tasks that are known to complete is a very short time without requiring the main thread. Event handlers
     * using this mode must return quickly to avoid blocking the posting thread, which may be the main thread.
     */
    POSTING,

    /**
     * Subscriber will be called in Android's main thread (sometimes referred to as UI thread). If the posting thread is
     * the main thread, event handler methods will be called directly. Event handlers using this mode must return
     * quickly to avoid blocking the main thread.
     */
    MAIN,

    /**
     * Subscriber will be called in a background thread. If posting thread is not the main thread, event handler methods
     * will be called directly in the posting thread. If the posting thread is the main thread, EventBus uses a single
     * background thread, that will deliver all its events sequentially. Event handlers using this mode should try to
     * return quickly to avoid blocking the background thread.
     */
    BACKGROUND,

    /**
     * Event handler methods are called in a separate thread. This is always independent from the posting thread and the
     * main thread. Posting events never wait for event handler methods using this mode. Event handler methods should
     * use this mode if their execution might take some time, e.g. for network access. Avoid triggering a large number
     * of long running asynchronous handler methods at the same time to limit the number of concurrent threads. EventBus
     * uses a thread pool to efficiently reuse threads from completed asynchronous event handler notifications.
     */
    ASYNC
}

我们通过配置,就可以让 EventBus类, 拿到具体的类型了


在看一下sticky() 我们默认不配置的时候,返回false(代码中可以知道) 我们例子里面,有一个地方, Activity还没有启动,只要配置sticky = true

再通过postSticky去传值,就可以传递到没有启动的Activity


priority() 我们暂时还没有用上, 我们通过看文档(或者对线程熟悉,名字就可以猜出来), 可以了解到和对应的优先级有关 这里先不扯了


简单总结

@interface Subscribe 还是挺简单的,就是3个方法,可以在配置中,传递3中参数 (每个都有默认值,所以也可以不配置对应的值)

拿到值以后,和上一篇一样, 应该有一个地方可以拿到对应的值,再做处理

代码地址 前面参考的代码,存放在(use_little_demo中的 eventbus3testhttps://github.com/2954722256/use_little_demo

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016.10.01 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • @interface Subscribe分析
    • 简单复习
      • 简单总结
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档