WorkManager:如何在同一应用程序中设置不同的WorkManager配置 [英] WorkManager : How to set-up different WorkManager Configurations in same App

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

问题描述

我正在处理一个多模块项目(Gradle模块).我在模块中使用WorkManager.我还利用Dagger进行依赖注入.现在,我必须使用匕首将依赖项注入到WorkManager中.我对WorkManager的Dagger 2设置非常熟悉.但是我面临的问题是,我必须使用工人工厂,使其与匕首兼容.这样我就可以借助Dagger Multi绑定注入依赖项.但是目前,主模块(主应用程序gradle"模块)中的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 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();
      }

不使用自定义工厂.并且已经有其他几个模块(用于其他功能的渐变模块)在没有工厂的情况下使用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

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(通常由自定义WorkerFactory处理)完全填充一个WorkRequest,它将回退到默认的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).

我们在DelegatingWorkerFactory /apps/iosched/shared/data/work/IoschedWorkerFactory.kt"rel =" nofollow noreferrer> IOsched ,用于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配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆