evenbus 四种模式

evenbus四种模式 POSTING、MAIN、BACKGROUND、ASYNC
POSTING:默认模式,在哪个线程发布消息就要在哪个线程接受消息
MAIN:指定接受者在主线程接受消息,无论是在哪个线程发布消息
BACKGROUND:指定接受者在子线程接受消息,主线程发布要在子线程接收消息,子线程发布还在他的线程接收消息
ASYNC:指定在子线程接受消息无论是什么线程发布。

evenbus只要处理接受事件的模式就可以,发布事件并不主要

源码:

private void postToSubscription(Subscription subscription, Object event, boolean isMainThread) {
        switch (subscription.subscriberMethod.threadMode) {
            case POSTING:
                invokeSubscriber(subscription, event);
                break;
            case MAIN:
                if (isMainThread) {
                    invokeSubscriber(subscription, event);
                } else {
                    mainThreadPoster.enqueue(subscription, event);
                }
                break;
            case MAIN_ORDERED:
                if (mainThreadPoster != null) {
                    mainThreadPoster.enqueue(subscription, event);
                } else {
                    // temporary: technically not correct as poster not decoupled from subscriber
                    invokeSubscriber(subscription, event);
                }
                break;
            case BACKGROUND:
                if (isMainThread) {
                    backgroundPoster.enqueue(subscription, event);
                } else {
                    invokeSubscriber(subscription, event);
                }
                break;
            case ASYNC:
                asyncPoster.enqueue(subscription, event);
                break;
            default:
                throw new IllegalStateException("Unknown thread mode: " + subscription.subscriberMethod.threadMode);
        }
    }

priority 属性 对于相同模式下,priority属性大的优先处理数据。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。