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個方法
我們可以發(fā)現勘天,對應的一些配置,例如



其實捉邢,都是通過注解傳入值的
(上一篇脯丝,我們也有對應的傳值例子,具體可以參考)


簡單復習

第一篇歌逢,我們提到過巾钉,有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()
我們暫時還沒有用上赤惊,
我們通過看文檔(或者對線程熟悉吼旧,名字就可以猜出來),
可以了解到和對應的優(yōu)先級有關
這里先不扯了


簡單總結

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

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

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


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

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市渺鹦,隨后出現的幾起案子扰法,更是在濱河造成了極大的恐慌,老刑警劉巖毅厚,帶你破解...
    沈念sama閱讀 219,188評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件塞颁,死亡現場離奇詭異,居然都是意外死亡卧斟,警方通過查閱死者的電腦和手機殴边,發(fā)現死者居然都...
    沈念sama閱讀 93,464評論 3 395
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來珍语,“玉大人锤岸,你說我怎么就攤上這事“逡遥” “怎么了是偷?”我有些...
    開封第一講書人閱讀 165,562評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長募逞。 經常有香客問我蛋铆,道長,這世上最難降的妖魔是什么放接? 我笑而不...
    開封第一講書人閱讀 58,893評論 1 295
  • 正文 為了忘掉前任刺啦,我火速辦了婚禮,結果婚禮上纠脾,老公的妹妹穿的比我還像新娘玛瘸。我一直安慰自己,他們只是感情好苟蹈,可當我...
    茶點故事閱讀 67,917評論 6 392
  • 文/花漫 我一把揭開白布糊渊。 她就那樣靜靜地躺著,像睡著了一般慧脱。 火紅的嫁衣襯著肌膚如雪渺绒。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,708評論 1 305
  • 那天,我揣著相機與錄音宗兼,去河邊找鬼躏鱼。 笑死,一個胖子當著我的面吹牛针炉,可吹牛的內容都是我干的挠他。 我是一名探鬼主播扳抽,決...
    沈念sama閱讀 40,430評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼篡帕,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了贸呢?” 一聲冷哼從身側響起镰烧,我...
    開封第一講書人閱讀 39,342評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎楞陷,沒想到半個月后怔鳖,有當地人在樹林里發(fā)現了一具尸體,經...
    沈念sama閱讀 45,801評論 1 317
  • 正文 獨居荒郊野嶺守林人離奇死亡固蛾,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,976評論 3 337
  • 正文 我和宋清朗相戀三年结执,在試婚紗的時候發(fā)現自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片艾凯。...
    茶點故事閱讀 40,115評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡献幔,死狀恐怖,靈堂內的尸體忽然破棺而出趾诗,到底是詐尸還是另有隱情蜡感,我是刑警寧澤,帶...
    沈念sama閱讀 35,804評論 5 346
  • 正文 年R本政府宣布恃泪,位于F島的核電站郑兴,受9級特大地震影響,放射性物質發(fā)生泄漏贝乎。R本人自食惡果不足惜情连,卻給世界環(huán)境...
    茶點故事閱讀 41,458評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望览效。 院中可真熱鬧却舀,春花似錦、人聲如沸朽肥。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,008評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽衡招。三九已至篱昔,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背州刽。 一陣腳步聲響...
    開封第一講書人閱讀 33,135評論 1 272
  • 我被黑心中介騙來泰國打工空执, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人穗椅。 一個月前我還...
    沈念sama閱讀 48,365評論 3 373
  • 正文 我出身青樓辨绊,卻偏偏與公主長得像,于是被迫代替她去往敵國和親匹表。 傳聞我的和親對象是個殘疾皇子门坷,可洞房花燭夜當晚...
    茶點故事閱讀 45,055評論 2 355

推薦閱讀更多精彩內容

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