yandexmobile / metrica-sample-android

Yandex AppMetrica SDK Sample for Android
50 stars 15 forks source link

Creates another instance of singleton object in its own process #47

Closed Rhtyme closed 5 years ago

Rhtyme commented 5 years ago

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.

alexklints commented 5 years ago

Hello.

Every process has own heap. So every singleton instance creates once per process. Also Application#onCreate() called once per process too. You should avoid initialization libraries like Room DB in every process. For more information please read documentation.