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


@interface Subscribe分析

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

我們通過eventbus3的注解


用ctrl+鼠標(biāo)左鍵, 跟進(jìn)去,可以看見
@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;
}

我們可以看見,和我們自己例子類似的結(jié)構(gòu)
這里多了個(gè)@Documentd, 這個(gè)也提到過,文檔相關(guān)的,我們可以忽略

簡(jiǎn)單扯淡
通過

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

我們可以知道,是放在方法上面使用的
(我們上一篇是放在類上面使用的,和這個(gè)不同的是,只是放在方法前面使用)
也就是上圖使用的形式

這里接口,有3個(gè)方法
我們可以發(fā)現(xiàn),對(duì)應(yīng)的一些配置,例如



其實(shí),都是通過注解傳入值的
(上一篇,我們也有對(duì)應(yīng)的傳值例子,具體可以參考)


簡(jiǎn)單復(fù)習(xí)

第一篇,我們提到過,有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()
我們默認(rèn)不配置的時(shí)候,返回false(代碼中可以知道)
我們例子里面,有一個(gè)地方,
Activity還沒有啟動(dòng),只要配置sticky = true


再通過postSticky去傳值,就可以傳遞到?jīng)]有啟動(dòng)的Activity


priority()
我們暫時(shí)還沒有用上,
我們通過看文檔(或者對(duì)線程熟悉,名字就可以猜出來),
可以了解到和對(duì)應(yīng)的優(yōu)先級(jí)有關(guān)
這里先不扯了


簡(jiǎn)單總結(jié)

@interface Subscribe
還是挺簡(jiǎn)單的,就是3個(gè)方法,可以在配置中,傳遞3中參數(shù)
(每個(gè)都有默認(rèn)值,所以也可以不配置對(duì)應(yīng)的值)

拿到值以后,和上一篇一樣,
應(yīng)該有一個(gè)地方可以拿到對(duì)應(yīng)的值,再做處理

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


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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,695評(píng)論 19 139
  • 原因 之前一直是用的 eventbus2.4版本eventbus很好用,因?yàn)閷?shí)現(xiàn)解耦了,所以用起來很方便但是,每次...
    dodo_lihao閱讀 1,143評(píng)論 5 18
  • Android 自定義View的各種姿勢(shì)1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,326評(píng)論 25 708
  • 因?yàn)椴粣鄱辉谝黄?,和因?yàn)椴荒芏辉谝黄穑欠N更痛苦? 見到允晴的時(shí)候,我心里是有些忐忑的,因?yàn)樵谀吧嗣媲爸v述這...
    樊雯婷閱讀 154評(píng)論 0 1
  • 有時(shí)候,人是不是會(huì)莫名的失憶,前半分鐘想的事,下一刻便忘記了 。 12點(diǎn)下課,食堂人太擠了,打算回宿舍點(diǎn)一份外面。...
    H啵妞閱讀 211評(píng)論 0 1

友情鏈接更多精彩內(nèi)容