【问题标题】:issue in while using `WorkContinuation.combine()` when WorkManger is initialized using dagger2使用 dagger2 初始化 WorkManger 时使用 `WorkContinuation.combine()` 时出现问题
【发布时间】:2019-07-22 13:41:59
【问题描述】:

我一直在使用 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()的代码吗?

标签: android kotlin dagger-2 android-architecture-components android-workmanager


【解决方案1】:

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

你有两个选择:

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

我个人更喜欢第二个选项,因为它可以处理 WorkManager 中会发生的任何变化,您可以添加更多/不同的 Worker Factories 来处理应用程序的不同部分/模块。

【讨论】:

  • 谢谢.. 我意识到经过一些研究,我从工厂中删除了throw IllegalArgumentException("unknown worker class name: $workerClassName") 这行代码,因为createWorker 方法返回nullable ListenableWorker 所以CombineContinuationsWorker 的实例不再由我的工厂处理..它可以工作..
【解决方案2】:

您应该从您的WorkerFactory 返回null 以委托给默认的WorkerFactory

不需要DelegatingWorkerFactory

【讨论】:

    猜你喜欢
    • 2021-11-13
    • 2020-08-17
    • 1970-01-01
    • 2021-01-09
    • 1970-01-01
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 2019-11-05
    相关资源
    最近更新 更多