Eventbus: 多次收到粘性事件

创建于 2013-11-25  ·  3评论  ·  资料来源: greenrobot/EventBus

在处理粘性事件时,我注意到我多次收到事件。 这就是同时发生事件的注册和发布的情况。 我快速查看了代码。 在 postSticky 方法中,我注意到 stickyEvents 对象上的锁在放置粘性事件后立即释放。 然后将事件发布到当前侦听器。
对于注册,首先注册侦听器以接收事件,然后尝试获取粘滞事件。 当它得到粘性事件时,它会收到它。 这意味着侦听器可以两次获得一个事件:

  1. 第一个是因为侦听器已经注册并且 postSticky 将事件发布到侦听器。
  2. 第二个是因为在注册粘性事件期间已经找到了 postSticky 放置的
need to investigate

最有用的评论

removeStickyEvent 可以解决这个问题
EventBus.getDefault().removeStickyEvent(NotifyToJoinEvent.class);

所有3条评论

你的问题解决了吗?

我使用了快速修复。 我在 EventBus 类上重载了 registerSticky(Object)、registerSticky(Object, int)、postSticky(Object) 方法。 并使它们同步,所以我避免了我上面描述的竞争条件。 因为这是一个快速修复,也许有更好的解决方法,所以我没有发送补丁。

public class EventBusWithStickyFix extends EventBus {

<strong i="8">@Override</strong>
public synchronized void registerSticky(Object subscriber) {
    super.registerSticky(subscriber);
}

<strong i="9">@Override</strong>
public synchronized void registerSticky(Object subscriber, int priority) {
    super.registerSticky(subscriber, priority);
}

<strong i="10">@Override</strong>
public synchronized void postSticky(Object event) {
    super.postSticky(event);
}

}

removeStickyEvent 可以解决这个问题
EventBus.getDefault().removeStickyEvent(NotifyToJoinEvent.class);

此页面是否有帮助?
0 / 5 - 0 等级