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

Event observers in singleton are registered multiple times #157

Open
goodsoft opened this issue Apr 8, 2015 · 6 comments
Open

Event observers in singleton are registered multiple times #157

goodsoft opened this issue Apr 8, 2015 · 6 comments
Milestone

Comments

@goodsoft
Copy link
Contributor

goodsoft commented Apr 8, 2015

Consider the following situation:

@Application
class App {
    @Inject Library lib;
}
@Activity
class Main {
    @Inject Library lib;
}
@Singleton
class Library {
    @Observes
    void onEvent (SomeEvent event) {
        // handle event
    }
}

In this situation event observer is registered and executed twice.
And each injection in each component registers yet another observer.

I've looked through generated code and I think the best way to solve this problem is to register observers for singletons not in component's onCreate method, but in singleton's ...$$UnscopedProvider.get method.
This would also solve the problem of observers' persistence between different activities, services etc

@johncarl81
Copy link
Owner

Where are you triggering the event from?

@goodsoft
Copy link
Contributor Author

goodsoft commented Apr 8, 2015

From entirely different object, instantiated by Library

@johncarl81
Copy link
Owner

I mean, where are you calling EventManager.trigger(someEvent)? From the App or Main class?

@goodsoft
Copy link
Contributor Author

goodsoft commented Apr 8, 2015

I mean exactly that.
There is an object of class, let's say, Socket, which is created in Library and then calls eventManager.trigger(someEvent).
The onEvent method is then called the same amount of times as the number of currently active (onResume called) components, which inject Library (i.e. current activity, application, several fragments)

@johncarl81
Copy link
Owner

Ah, ok, I understand and you are probably right. Not sure what the fix would be immediately, but I do like your idea about registering the observer in the Provider.

@johncarl81
Copy link
Owner

@goodsoft, in case it wasn't obvious, the stop-gap solution would be the following:

@Application
class App {
    @Inject Library lib;

    @Observes
    void onEvent (SomeEvent event) {
        lib.onEvent(event);
    }
}
@Activity
class Main {
    @Inject Library lib;
}
@Singleton
class Library {
    void onEvent (SomeEvent event) {
        // handle event
    }
}

... until we have a better solution for this

@johncarl81 johncarl81 added this to the 0.3.0 Release milestone Aug 24, 2016
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

No branches or pull requests

2 participants