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


@interface Subscribe分析

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

我们通过eventbus3的注解

Eventbus3代码分析(四):@interface Subscribe分析_第1张图片

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

@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

/**
 * 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
Eventbus3代码分析(四):@interface Subscribe分析_第2张图片


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


简单总结

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

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

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


下一篇我们可以了解Eventbus3代码分析(五):getDefault(),register和EventBusBuilder等

你可能感兴趣的:(Eventbus3代码分析(四):@interface Subscribe分析)