Creates another instance of singleton object in its own process
Rhtyme opened this issue · 2 comments
I am using your library by initialising in the onCreate() method of the Application class as follows:
// Creating an extended library configuration. val config = YandexMetricaConfig.newConfigBuilder(getString(yandex_metrika_api_key)).build() // Initializing the AppMetrica SDK. YandexMetrica.activate(applicationContext, config) // Automatic tracking of user activity. YandexMetrica.enableActivityAutoTracking(this)
My project uses koin as a dependency injection, and I have class TaskManager class that I announced as a singleton in koin module. I inject this class in activities as well in Application class, because I need trigger its synchronize() function on network change. The problem is that, when I initialise YandexMetrica, the library creates the instance of TaskManager, and calls synchronize() method by itself and here the problems start. Because I have a queue which writes/reads data to Room DB and synchronises with network. Because of such unexpected behaviour, my queue is broken. Can you please explain why it tries to recreate the objects declared in Application in its own process (I know that YandexMetrica works in a different process) even if the objects are declared as singletones? Thanks for your response in advance.
Hello.
It is default android workflow. Each process has separate isolated memory and jvm instance. So, singletons from main process are inaccessible. That's why Application#onCreate() called once for each process.
AppMetrica SDK documentation contains recommendation to resolve similar problems.
You can use one of popular variants for detecting main process. For example: https://stackoverflow.com/questions/19631894/is-there-a-way-to-get-current-process-name-in-android.
Hello,
thanks I managed to solve the issue by initializing the components only in main process by checking if the current process is the main one. Thanks for your response