Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Koin injecting into WorkManager

I have a basic work manager

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

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

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.

like image 555
Daniel Sims Avatar asked Aug 04 '19 18:08

Daniel Sims


1 Answers

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()
    }
}
like image 58
Daniel Sims Avatar answered Sep 23 '22 04:09

Daniel Sims