Skip to content Skip to sidebar Skip to footer

Koin Injecting Into Workmanager

I have a basic work manager class BackgroundSyncWorker ( appContext: Context, workerParams: WorkerParameters ): Worker(appContext, workerParams) { override fun doWork

Solution 1:

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

classBackgroundSyncWorker(
    appContext: Context,
    workerParams: WorkerParameters
): Worker(appContext, workerParams), KoinComponent {

    val dataSyncRepository : DataSyncRepositoryImpl by inject()

    overridefundoWork(): Result {
        return Result.success()
    }
}

Solution 2:

I have noticed a couple of things from your code:

The first reason for why this does not work because you need to extend/inherit the BackgroundSyncWork from KoinComponent, so making this BackgroundSyncWork koin-aware.

classBackgroundSyncWorker(
    appContext: Context,
    workerParams: WorkerParameters
): Worker(appContext, workerParams), KoinComponent {

val database: Database by inject()

    overridefundoWork(): Result {
        return Result.success()
    }
}

Second: Also, please make sure that database object creation is properly configured in koin module. It should work with no problem.

Post a Comment for "Koin Injecting Into Workmanager"