androidgreenrobot-eventbus

Posting Objects through Event Bus Post


How many events should i post for a single Event Bus? For Example -

Registering Event Bus - EventBus.getDefault().register(this);

Posting Event - EventBus.getDefault().post(Object);

And is there any problem causes if i could not unregister the EventBus


Solution

  • There's no limitations to number of events in EventBus.

    If the lifetime of the object you've registered to the EventBus is shorter than the lifetime of the EventBus (which is typically the same as application lifetime), you definitely need to unregister from EventBus. If you don't, the registered object will always have a reference to it inside the EventBus which will prevent the garbage collector from doing its job.

    Say you have a file viewer activity which subscribes to an event. You can open a file, look at it, close the activity and open another file which uses the same activity. Now if you don't unsubscribe from EventBus all of the activities, that the user opens, will have a reference to them inside EventBus. They will never be garbage collected and so eventually the application would run out of memory.