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


@interface Subscribe分析

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

我們通過eventbus3的注解


用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


priority()
我們暫時還沒有用上,
我們通過看文檔(或者對線程熟悉,名字就可以猜出來),
可以了解到和對應的優先級有關
這里先不扯了


簡單總結

@interface Subscribe
還是挺簡單的,就是3個方法,可以在配置中,傳遞3中參數
(每個都有默認值,所以也可以不配置對應的值)

拿到值以后,和上一篇一樣,
應該有一個地方可以拿到對應的值,再做處理

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


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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容

  • Spring Cloud為開發人員提供了快速構建分布式系統中一些常見模式的工具(例如配置管理,服務發現,斷路器,智...
    卡卡羅2017閱讀 134,948評論 18 139
  • 原因 之前一直是用的 eventbus2.4版本eventbus很好用,因為實現解耦了,所以用起來很方便但是,每次...
    dodo_lihao閱讀 1,108評論 5 18
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 173,357評論 25 708
  • 因為不愛而不在一起,和因為不能而不在一起,那種更痛苦? 見到允晴的時候,我心里是有些忐忑的,因為在陌生人面前講述這...
    樊雯婷閱讀 134評論 0 1
  • 有時候,人是不是會莫名的失憶,前半分鐘想的事,下一刻便忘記了 。 12點下課,食堂人太擠了,打算回宿舍點一份外面。...
    H啵妞閱讀 198評論 0 1