EvenBus3.0中文翻译(二)Priorities and Event Cancellation

来源:互联网 发布:如何卸载mac上的app 编辑:程序博客网 时间:2024/06/09 03:08

水平有限,仅供参考

原文地址

Priorities and Event Cancellation

重点与事件取消

While most use cases of EventBus do not need priorities nor event cancellation,

they may come in handy in some special cases. For example,

an event may trigger some UI logic if the app is in the foreground, 

but react differently if the app is currently not visible to the user.

虽然EventBus 大多数情况下不需要优先级或者取消,但是他们可能会在特殊场景用到。

例如,如果APP程序是正在前台展示,这个事件可能会引起一些UI界面的变化,

但是,APP程序处于后台不可见,又会引起不同的反应。

Subscriber Priorities

订阅者优先级

You may change the order of event delivery by providing a priority to the subscriber during registration.

您可以在注册过程中设置订阅者不同的优先级来改变事件接收顺序。

@Subscribe(priority = 1);public void onEvent(MessageEvent event) {…}

Within the same delivery thread (ThreadMode),

 higher priority subscribers will receive events before others with a lower priority. The default priority is 0.

在同一个线程模式(ThreadMode)里,

优先级高的订阅者将比优先级低的订阅者优先接收到事件。默认优先级为0

Note: the priority does NOT affect the order of delivery among subscribers with different ThreadModes!

注:优先级并不能影响到不同线程模式(ThreadMode)里面的订阅者!

Cancelling event delivery

取消事件传递

You may cancel the event delivery process by calling cancelEventDelivery(Object event)

 from a subscribers event handling method. 

Any further event delivery will be cancelled: subsequent subscribers wont receive the event.

你可以通过调用订阅者的事件处理方法cancelEventDelivery(Object event)取消事件传递过程。

任何进一步的事件传递都将被取消:随后的订阅者将无法接收到事件。

// Called in the same thread (default)// 在同一线程中(默认)@Subscribepublic void onEvent(MessageEvent event){// Process the event// 处理事件…EventBus.getDefault().cancelEventDelivery(event) ;}

Events are usually cancelled by higher priority subscribers.

 Cancelling is restricted to event handling methods running in posting thread ThreadMode.PostThread.

事件通常被优先级高的订阅者取消。事件取消方法仅限于运行在发布线程ThreadMode.PostThread




1 0
原创粉丝点击