dudeck
dudeck

Reputation: 413

Second initialization of WorkManager

I have to initialize WorkerManager not only once because at Runtime some arguments may change (WorkerFactory()). So I need to call

val configuration = Configuration.Builder()
            .setWorkerFactory(aggregatingWorkerFactory)
            .build()

WorkManager.initialize(context!!, configuration)

However, calling it second time throws IllegalStateException.

I read that I can initialize it on ContentProvider: https://medium.com/@programmerr47/custom-work-manager-initialization-efdd2afa6459 but my workerFactory object is injected by dagger and I couldn't use AndroidInjector on Content Provider, because it was always not initialized:

Caused by: kotlin.UninitializedPropertyAccessException: lateinit property androidInjector has not been initialized

I read here that there is an interface HasContentProviderInjector:
but I cannot find it in my Dagger2 version 2.24 and I don't believe it should work, because If I understand correctly ContentProvider is created before Application object.

What should I do? Try to clear by reflection objects: sDelegatedInstance != null && sDefaultInstance != null or sth else?

Upvotes: 1

Views: 736

Answers (1)

pfmaggi
pfmaggi

Reputation: 6476

WorkManager is a singleton and it can only be initialized once.
You can have the default initialization or your own initialization. To have your own configuration you need to disable the default one as described in the documentation.

Upvotes: 1

Related Questions