在WorkManager的Worker中注入存储库

时间:2018-10-25 10:24:56

标签: android multithreading mvvm dependency-injection concurrency

用例:我需要征询用户的反馈意见,并将其保存在永久性存储(在本例中为“会议室”)中。每隔X分钟,我想将这些存储的FeedbackRequest发送到后端。

解决方案:我考虑使用WorkManager,因为此操作应在过程终止后继续存在并定期触发。我需要从Room DB获取FeedbackRequest的列表,并使用Retrofit客户端将每个FeedbackRequest发送到后端。

问题定义FeedbackRequestRepository(负责获取FeedbackRequest的列表)是WorkManager的doWork()方法中必需的,用于获取Room DB中的数据。另外,我对Dagger2还很陌生。打开应用后,我收到上述错误。

  

AppComponent代码段

interface AppComponent {
@Component.Builder
interface Builder {
    @BindsInstance
    fun application(application: Application): Builder

    fun build(): AppComponent
}

fun inject(scFeedbackApp: ScFeedbackApp)
fun inject(dispatchWorker: DispatchWorker)

}

  

存储库代码段

@Singleton
class FeedbackRequestRepository @Inject constructor(
        private val appExecutors: AppExecutors,
        private val db: ScFeedbackDB,
        private val feedbackAcknowledgeDao: FeedbackRequestDao,
        private val scFeedbackFeedbackService: SCFeedbackService
) {

...

}
  

工人代码段

class DispatchWorker @Inject constructor(context:Context,
                             workerParameters: WorkerParameters,
                             var feedbackRequestRepository: FeedbackRequestRepository):
    Worker(context, workerParameters){
init {
    DaggerAppComponent.builder().application(context.applicationContext as ScFeedbackApp)
            .build().inject(this)
}
override fun doWork(): Result {

...

}
  

错误

E/WorkerFactory: Could not instantiate com.scfeedback.service.responseDispatcher.DispatchWorker
java.lang.NoSuchMethodException: <init> [class android.content.Context, class androidx.work.WorkerParameters]
    at java.lang.Class.getConstructor(Class.java:528)
    at java.lang.Class.getDeclaredConstructor(Class.java:507)
    at androidx.work.WorkerFactory.createWorkerWithDefaultFallback(WorkerFactory.java:85)
    at androidx.work.impl.WorkerWrapper.runWorker(WorkerWrapper.java:188)
    at androidx.work.impl.WorkerWrapper.run(WorkerWrapper.java:124)
    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)
E/WorkerWrapper: Could not create Worker com.scfeedback.service.responseDispatcher.DispatchWorker
  1. 这与“依赖注入”设置有关。
  2. 如果不是,那我要去哪里错了。

1 个答案:

答案 0 :(得分:0)

您的方法有两个问题:

  • 不能同时使用构造函数注入@inject constructor(...)和直接注入DaggerAppComponent...inject(this)
  • 您必须使用默认构造函数:

因此您可以将DispatchWorker更改为:

    class DispatchWorker constructor(context:Context,
                                 workerParameters: WorkerParameters):
        Worker(context, workerParameters){

        @Inject  lateinit var feedbackRequestRepository: FeedbackRequestRepository

    init {
        DaggerAppComponent.builder().application(context.applicationContext as ScFeedbackApp)
                .build().inject(this)
    }
    override fun doWork(): Result {

    ...

}

或使用custom WorkerFactory