简体   繁体   中英

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?

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM