@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等