Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature proposal: Partial registering and unregistering #471

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

danielaRiesgo
Copy link

@danielaRiesgo danielaRiesgo commented Sep 15, 2017

The PR proposes a registering and unregistering of a subscriber specifically for certain event types.

I think this will help with certain cases were you want to stop listening to some event without stop listening to all the other events, or start listening to some event only after something happens. For example, when you need to make a request to the server but you don't have internet, and so you want to start hearing an InternetConnection event or something that tells you when the internet is back on, so that you can make that request. But after you've done that request, you don't need to keep listening at all.

 @Subscribe 
 public void onInternetConnection(NetworkConnection event) {
     if (event.isConnected()) {
         makeRequest();
         EventBus.getDefault().unregister(this, NetworkConnection);
     }
 }

 @Subscribe 
 public void onSomeOtherEvent(MyEvent event) { /* Do something */ };

 @Override
 public void onStart() {
     super.onStart();
     EventBus.getDefault().register(this);
 }

 @Override
 public void onStop() {
     super.onStop();
     EventBus.getDefault().unregister(this);
 }

Hope you find it useful too.


By the way, I had to do what the fix-empty-test-suite branch said to make the tests run.

@greenrobot
Copy link
Owner

Thanks. Well, we had a feature for registering only certain event types before and kicked it out for simplicity. You can still have a inner class for example to receive specific event classes only...

@danielaRiesgo
Copy link
Author

Yes, but I think it's not good to change your structure creating new virtual classes to adapt yourself to what you would want the framework to do.
Once we have this partial registration, then from your part, you can do some things to add features you may want without altering your structure. Like registering a list of events, or registering all events except some list of events. (Which, by the way, I would also think that the framework could provide to be more complete and user friendly, but that may be nicetohave.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants