Open goodsoft opened 9 years ago
Where are you triggering the event from?
From entirely different object, instantiated by Library
I mean, where are you calling EventManager.trigger(someEvent)
? From the App
or Main
class?
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)
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.
@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
Consider the following situation:
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