WorkManager : How to set-up different WorkManager Configurations in same App

萝らか妹 提交于 2021-01-04 05:43:09

问题


I'm working on a multi module project (Gradle module). I'm using WorkManager in my module. I'm also making use of Dagger for dependency injection. Now I have to use dagger to inject dependencies to my WorkManager. I'm quite familiar with Dagger 2 setup with WorkManager. But the problem I'm facing is, I have to use worker factory to make it compatible with dagger. So that I can inject dependencies with the help of Dagger Multi bindings. But currently the WorkManager configuration in the main module (Main app gradle module) is

      public Configuration getWorkManagerConfiguration() {
        return new Configuration.Builder()
            .setMinimumLoggingLevel(android.util.Log.INFO)
            .build();
      }

Which doesn't use a custom factory. And already several other modules (gradle modules for other features) are using WorkManger without factory. Now If I change this configuration and add a factory, it might break the work manager setup in several other place. Can I make use of a factory only for the WorkManager classes in my module (or only some work manager classes should be initialized via factory, others use default configuration). Is it possible to do? Hope my problem is clear.


回答1:


You can use a DelegatingWorkerFactory and add you're custom WorkerFactory to it.

Your custom WorkerFactory will need to check if the classname passed to the factory is the one it want to handle, if not, just return null and the DelegatingWorkerFactory will revert to the default worker factory using reflection.

Keep in mind that you need to add your custom WorkerFactory each time you initialize WorkManager. If you don't do that and WorkManager tries to fullfill a WorkRequest for your Worker (that is normally handled by the custom WorkerFactory) it will fallback to the default WorkerFactory and fail (probably with a class not found exception).

We are using the DelegatingWorkerFactory in IOsched, the scheduling app used for I/O and the Android Developer Summit. The code of your custom WorkerFactory is going to be something like:

class ConferenceDataWorkerFactory(
    private val refreshEventDataUseCase: RefreshConferenceDataUseCase
) : WorkerFactory() {

    override fun createWorker(
        appContext: Context,
        workerClassName: String,
        workerParameters: WorkerParameters
    ): ListenableWorker? {

        return when (workerClassName) {
            ConferenceDataWorker::class.java.name ->
                ConferenceDataWorker(appContext, workerParameters, refreshEventDataUseCase)
            else ->
                // Return null, so that the base class can delegate to the default WorkerFactory.
                null
        }
    }
}


来源:https://stackoverflow.com/questions/59942384/workmanager-how-to-set-up-different-workmanager-configurations-in-same-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!