16

I use greenrobot EventBus library to send data between two fragments in my android app and I want to know what is the diffeence between register(Object b) method and registerSticky(Object object) method?

karoluch
  • 653
  • 3
  • 9
  • 21

1 Answers1

49

EventBus allows you to post events that are "sticky" and by that EventBus understands events that "stick to the eventbus" for future access.

If you post a normal event when there are no subscribers registered at the moment of sending, this event will be discarded.

You can post a sticky event though, even if there are no subscribers to receive that at the moment, and it won't be discarded (unless there is another sticky event posted in the future). When a subscriber registers with registerSticky the delivery of the last sticky event is also triggered.

Bartek Lipinski
  • 30,698
  • 10
  • 94
  • 132
  • Can I use `resgisterSticky` instead of `register` all time? – karoluch Jan 20 '15 at 18:14
  • 1
    Although `registerSticky` acts like regular register (and triggers the delivery of last `sticky` `event`) I think it should be avoided as long as you don't see a serious case for using `sticky event` – Bartek Lipinski Jan 20 '15 at 18:26
  • 1
    @blipinsk Why avoid? Please elaborate... Can I mix the two register and registerSticky/post and postSticky? – powder366 Aug 26 '15 at 11:23
  • @powder366 Sure thing you can mix the two of them. As long as you see a case for that and as long as you don't mean registering the same object to the same `EventBus` instance with both methods (`register` and `registerSticky`) at the same time. Because in that case mixing those two doesn't make any sense (only `registerSticky` would give the same result). – Bartek Lipinski Aug 26 '15 at 12:23
  • @powder366 You should avoid using `registerSticky` (for a **regular** registering to `EventBus` - without a serious case for `registerSticky`) because it could lead to extremely hard to debug situations. Imagine you have some sort of short-life event (which could for example represent a reaction to user click). It would be possible for you to receive an old event (that has been posted with `postSticky` a relatively long time ago) and you would treat it as an event that has been posted right now. – Bartek Lipinski Aug 26 '15 at 12:26
  • 1
    This text should replace this "crap" http://greenrobot.org/eventbus/documentation/configuration/sticky-events/ so people like me would understand it :) – Farid Oct 05 '18 at 15:43