使用 dagger2 初始化 WorkManger 时使用 `WorkContinuation.combine()` 时出现问题

issue in while using `WorkContinuation.combine()` when WorkManger is initialized using dagger2

我一直在使用 workManager,它在 dagger2 上运行良好,但是当我使用 WorkContinuation.combine() 进行链接时,WorkerFactory 出现问题 我的工人没有初始化

崩溃报告是

java.lang.IllegalArgumentException: unknown worker class name: androidx.work.impl.workers.CombineContinuationsWorker
        at incubasys.aydo.repositories.worker.WorkerFactory.createWorker(WorkerFactory.kt:22)
        at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:81)
        at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:228)
        at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:127)
        at androidx.work.impl.utils.SerialExecutor$Task.run(SerialExecutor.java:75)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
        at java.lang.Thread.run(Thread.java:818)

崩溃发生的WorkerFactory

class DaggerWorkerFactory @Inject constructor(
    private val workerFactories: Map<Class<out ListenableWorker>, @JvmSuppressWildcards Provider<ChildWorkerFactory>>
) : WorkerFactory() {
    override fun createWorker(
        appContext: Context,
        workerClassName: String,
        workerParameters: WorkerParameters
    ): ListenableWorker? {
        val foundEntry =
            workerFactories.entries.find { Class.forName(workerClassName).isAssignableFrom(it.key) }
//crashes here
        val factoryProvider = foundEntry?.value
            ?: throw IllegalArgumentException("unknown worker class name: $workerClassName")
        return factoryProvider.get().create(appContext, workerParameters)
    }
}

它只会在我使用 WorkContinuation.combine() 时崩溃。

WorkContinuation.combine()CombineContinuationsWorker class 的一个实例添加到您的延续中。
这意味着您的 WorkerFactory 在创建实际工作人员 class 时需要处理此 class 名称。

你有两个选择:

  1. 你处理 CombineContinuationsWorker 到你的 workerFactory
  2. 您使用 delegatingWorkerFactory 并将您的工厂添加到其中,以便在找不到正确的 class 名称时 returns null。 然后 delegatingWorkerFactory 会使用默认的 workerFactory 来实例化对 class.

我个人更喜欢第二个选项,因为它可以处理 WorkManager 中的任何更改,并且您可以添加 more/different Worker Factories 来处理不同的 pieces/module 应用程序。

您应该 return null 从您的 WorkerFactory 委派给默认值 WorkerFactory

不需要 DelegatingWorkerFactory