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

Stop exception spam on register and unregister methods #645

Open
hansgeorgSchwibbe opened this issue Jun 25, 2020 · 1 comment
Open

Stop exception spam on register and unregister methods #645

hansgeorgSchwibbe opened this issue Jun 25, 2020 · 1 comment
Labels
enhancement New feature or request

Comments

@hansgeorgSchwibbe
Copy link

hansgeorgSchwibbe commented Jun 25, 2020

The Story
Expect a singleton dialog instance which will be opened by the user on a specific button click.
This singleton dialog instance registers on the eventbus when the dialog starts to show.
What the developer now has to consider is to check if the dialog instance is already registered.
Otherwise an exception will be thrown which is a common pitfall.
It brings the application into an unecessary error state.
And it leads to boiler plate code which itself leads to bad design, because developers have to implement:

if (!eventBus.isRegistered(object)) {
    eventBus.register(object);
}

and

if (eventBus.isRegistered(object)) {
    eventBus.unregister(object);
}

Solution Proposal
Why not implementing the register and unregister methods like this:

    /**
     * Registers on event bus.
     *
     * @param object the object to register
     * @return true if the object is registered successfully, false if the object was already registered.
     */
    public boolean registerBetter(final Object object) {
        if (!isRegistered(object)) {
            register(object);
            return true;
        }
        return false;
    }

    /**
     * Unregisters on event bus.
     *
     * @param object the object to unregister
     * @return true if the object unregistered successfully, false if the object was not registered before.
     */
    public boolean unregisterBetter(final Object object) {
        if (isRegistered(object)) {
            unregister(object);
            return true;
        }
        return false;
    }

With that solution developers can just call register, and they don't have to consider the internal state of the event bus.
Because the internal state of the event bus framework is something that the framework should handle, not the developer who is using it.

@tomridder
Copy link

i guess it can't be changed like u said
because eventbus will post stick event when execute register method.
so if there 's a stick event in ur dialog and it need to be executed again and again ,your suggestion will stop it from executing again.
i guess that's why greenbot give a interface of isRegistered

@greenrobot-team greenrobot-team added the enhancement New feature or request label Mar 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants