本文介绍了WorkManager : 如何在同一个应用程序中设置不同的 WorkManager 配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个多模块项目(Gradle 模块).我在我的模块中使用 WorkManager.我也在使用 Dagger 进行依赖注入.现在我必须使用 dagger 将依赖项注入到我的 WorkManager 中.我非常熟悉使用 WorkManager 设置 Dagger 2.但我面临的问题是,我必须使用工人工厂使其与匕首兼容.这样我就可以在 Dagger Multi 绑定的帮助下注入依赖项.但是目前主模块(Main app gradle module)中的WorkManager配置是

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 useworker 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();
      }

不使用自定义工厂.并且已经有几个其他模块(其他功能的 gradle 模块)在没有工厂的情况下使用 WorkManger.现在,如果我更改此配置并添加工厂,则可能会破坏其他几个地方的工作管理器设置.我可以只为模块中的 WorkManager 类使用工厂吗(或者只有一些工作管理器类应该通过工厂初始化,其他使用默认配置).有可能吗?希望我的问题很清楚.

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.

推荐答案

您可以使用 DelegatingWorkerFactory添加 你是自定义的 WorkerFactory .

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

您的自定义 WorkerFactory 将需要检查传递给工厂的类名是否是它要处理的类名,如果不是,只需返回 null 并且 DelegatingWorkerFactory 将恢复使用反射到默认的工人工厂.

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.

请记住,每次初始化 WorkManager 时都需要添加自定义的 WorkerFactory.如果您不这样做并且 WorkManager 尝试为您的 Worker 填充 WorkRequest(通常由自定义 WorkerFactory 处理),它将回退到默认的 WorkerFactory 并失败(可能会出现类未找到异常).

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).

我们在 ,用于 I/O 和 Android 开发者峰会的调度应用程序.您的自定义 WorkerFactory 的代码将类似于:

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
        }
    }
}

这篇关于WorkManager : 如何在同一个应用程序中设置不同的 WorkManager 配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-27 09:12