EventBus3.0-优先级和取消事件

在EventBus使用过程中一般不需要使用优先级和取消事件,这两个方法用来处理特殊情况。例如,一个事件在app处于前台时要触发ui逻辑;但当app处于后台,则要有不一样的响应。

订阅者优先级(Subscriber Priorities)

你可以通过提升优先级来修改事件传递的顺序。

@Subscribe(priority =1);

publicvoidonEvent(MessageEventevent) {…}


在相同的线程模式下,高优先级的订阅者将会比低优先级的订阅者更早接收到事件。EventBus默认的优先级是0。 

注意:优先级不会影响不同线程模式下的订阅者接收事件的顺序。

取消事件(Cancelling event delivery)

你可以在订阅者的事件处理方法中通过调用cancelEventDelivery去中断事件传递的过程。任何下一步的事件都会被取消;之后的订阅者也不会收到事件。

// Called in the same thread (default)@SubscribepublicvoidonEvent(MessageEventevent){// Process the event…EventBus.getDefault().cancelEventDelivery(event) ;}

事件一般都是被高优先级的订阅者取消传递的。事件取消仅限于ThreadMode.PostThread下才可以使用。

同时多个消息接受者,默认有调用顺序之差吗?

转载于:https://www.jianshu.com/p/d68a4091e2e0

猜你喜欢

转载自blog.csdn.net/weixin_34090643/article/details/91141320