EventBus icon indicating copy to clipboard operation
EventBus copied to clipboard

Trigger ALL sticky events on registerSticky()

Open ghost opened this issue 11 years ago • 9 comments

It would be great if registerSticky() method trigger ALL sticky events current class is subscribed to. My case: activity (sender), and fragment (built into layout) inside it (receiver). Activity sends bunch of events from onCreate, but, of course, they should be posted as sticky as there still no live fragment. When fragment is fully created it is registered to the EventBus, but now I have to check if there are sticky events for ALL event types this fragment is subscribed to. Very ugly. It would be much better if registerSticky() trigger delivery of all sticky events sent to current class.

ghost avatar May 27 '14 07:05 ghost

registerSticky does deliver all sticky events. Is it a timing issue on your side?

If you think there's a bug, please try to isolate it.

greenrobot avatar May 27 '14 08:05 greenrobot

According to the method description and to the (as far as I understand it) code, registerSticky delivers only one (last?) sticky event for current subscriber:

Like register(Object), but also triggers delivery of the most recent sticky event (not events) (posted with postSticky(Object)) to the given subscriber.

if (sticky) { Object stickyEvent; synchronized (stickyEvents) { stickyEvent = stickyEvents.get(eventType); } if (stickyEvent != null) { postToSubscription(newSubscription, stickyEvent, Looper.getMainLooper() == Looper.myLooper()); } }

But as far as I understand there can be more than one sticky events if they are of different types.

ghost avatar May 27 '14 08:05 ghost

I agree, It would be useful.

nunocm avatar Apr 24 '15 12:04 nunocm

I agree too! If only the last sticky event delivered to the subscriber, it means sometimes maybe miss some useful event!

qianzui avatar May 08 '15 07:05 qianzui

+1

C0nstan avatar Mar 01 '16 12:03 C0nstan

Meaning, keeping a queue of sticky event per event class. And then, we would need, methods for

  1. clearing this queue (per event class)
  2. get all events
  3. get last event

Does anyone knows of a fork based on version 3 that implemented this ?

miroslavign avatar Jan 19 '17 13:01 miroslavign

Or maybe, just keep a counter of how many sticky events were fired, until a subscriber subscribed to this event class instance. So, saving only the last one, BUT knowing there were more, a user can decide to take some extra steps in, say, refreshing a view.

miroslavign avatar Jan 19 '17 13:01 miroslavign

Sounds to me like you need proper persistence (a queue), like a log file or database. Or as said above, create an in-memory queue that you access if required. -ut

greenrobot-team avatar Jan 23 '17 14:01 greenrobot-team

Yes, i started a few commits on my fork. After i create something that works, I will post it here, or create a pull request. So If it is in compliance with your code, other may benefit from it.

miroslavign avatar Jan 23 '17 14:01 miroslavign