2

I have a question regard Android EventBus events.

Is there any meaning to annotate an event bus subscriber callback method with sticky = true if the event was posted as none sticky?

I've used eventBus.post(new MyEvent()) and not eventBus.postSticky(new MyEvent()) to fire the event.

Moti Bartov
  • 3,454
  • 33
  • 42

1 Answers1

2

(Answer written for v3.0.0 of EventBus).


As far as I can tell, no event will be passed to the subscription method when it registers in this scenario.

When a new subscription method is processed it is checked to see whether or not it was annotated with sticky = true. EventBus then iterates over all events previously posted using postSticky and delivers a saved sticky event if appropriate (i.e. if an event of the correct Java type is found).

In your particular scenario, no sticky event of the appropriate type will be found during the iteration phase, so nothing will be delivered to the subscription method when it registers despite the sticky = true annotation. The method should still receive all events posted after it registers.

If you want to double-check my parsing of the EventBus code, it should be quick to put together a small sample app that tests your scenario in isolation.

Hope that helps!

stkent
  • 19,772
  • 14
  • 85
  • 111
  • Thanks for quick answer. When u say processed it means on the register moment right? So according to this, it will be useless to use removeStickyEvent is the method that subscribed to the events right? – Moti Bartov Jul 24 '18 at 08:39
  • 1
    Yep, the processing occurs when you call [`register`](https://github.com/greenrobot/EventBus/blob/V3.0.0/EventBus/src/org/greenrobot/eventbus/EventBus.java#L134). Calling [`removeStickyEvent`](https://github.com/greenrobot/EventBus/blob/V3.0.0/EventBus/src/org/greenrobot/eventbus/EventBus.java#L322) won't do anything as there is no saved sticky event of the appropriate Java type to remove :) – stkent Jul 24 '18 at 08:53
  • Thanks, Another small question, what is the preferred way to do 'removeStickyEvent'? 1. 'removeStickyEvent(event)' //the accepted event in the callaback 2. 'removeStickyEvent(MyEvent.class)' use the class type – Moti Bartov Jul 24 '18 at 11:18
  • 1
    I don't think either is strongly preferred over the other - both are fine alternatives! I'd probably use the former if I was unsubscribing in response to a received event, and the latter if I was unsubscribing at some other time that is unrelated to the receipt of an individual event. – stkent Jul 24 '18 at 11:22
  • So, if for instance, If I want to remove only the received event, I should use the former, but if I want to eliminate all events from this type once I get the first one, I should use the later right? I am registering to an event at only one location in the app, and I want to prevent any possible double events that may occur. – Moti Bartov Jul 25 '18 at 07:08
  • 1
    There is only ever one sticky event (of a given type) at a time, so either method has the effect of removing the sticky event so that no other subscribers will receive it. The two versions of the remove method are provided purely for convenience: see https://github.com/greenrobot/EventBus/blob/V3.0.0/EventBus/src/org/greenrobot/eventbus/EventBus.java#L313 vs https://github.com/greenrobot/EventBus/blob/V3.0.0/EventBus/src/org/greenrobot/eventbus/EventBus.java#L327 – stkent Jul 25 '18 at 07:35