Koin注入WorkManager [英] Koin injecting into WorkManager

查看:131
本文介绍了Koin注入WorkManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基本的工作经理

I have a basic work manager

class BackgroundSyncWorker (
    appContext: Context,
    workerParams: WorkerParameters
): Worker(appContext, workerParams) {

    override fun doWork(): Result {
        return Result.success()
    }
}

我想将我的存储库注入其中以在数据库中做一些工作.我已经正确设置了Koin,但似乎找不到找到将我的依赖项注入Worker的方法.我已经尝试继承KoinComponent并尝试使用它来实现它,但是by inject()不存在,但是有两个by inject方法我找不到使用方法.似乎没有任何有关如何注入管理者的信息,尽管有一些使用匕首的方法.

And I want to inject my repository into this to do some work in my database. I've set Koin up correctly but can't seem to find a way of how to inject my dependency into the Worker. I've tried inheriting the KoinComponent and trying to do it using that, but by inject() doesn't exist, but there's two by inject methods that I can't find how to use. There doesn't seem to be any information on how to inject into managers, although there's a few for using dagger.

推荐答案

这确实有效,我只是使用var而不是val.

This does actually work, I was just using var instead of val.

class BackgroundSyncWorker (
    appContext: Context,
    workerParams: WorkerParameters
): Worker(appContext, workerParams), KoinComponent {

    val dataSyncRepository : DataSyncRepositoryImpl by inject()

    override fun doWork(): Result {
        return Result.success()
    }
}

这篇关于Koin注入WorkManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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