I am trying to use work manager and use Koin to get some dependencies I have setup. My work manager extends KoinComponent
which then allows me to use by inject
but every time I try to use a component I am trying to get I get the error
NoBeanDefFoundException: No definition found for class AuthenticationService. Check your definitions!
Bear in mind that I use these dependencies just fine in activities and view models
My work manager
class BackgroundSync(private val context: Context, workerParams: WorkerParameters):CoroutineWorker(context, workerParams),
KoinComponent{
override suspend fun doWork(): Result {
val authService:AuthenticationService by inject()
val token = authService.getAuthToken() // Error here when trying to use it
}
}
Then in my Koin module setup I have this
private val myModule = module {
single<IAuthenticationService> { AuthenticationService() }
}
I used this question as reference but I cant get it to work properly, any idea's in what I am doing wrong?
In Koin you should inject exactly what you provide. In your case, in the koin module, you provide an interface but in BackgroundSync
you inject the concrete class.
I believe you need to inject the interface:
class BackgroundSync(private val context: Context, workerParams: WorkerParameters):CoroutineWorker(context, workerParams),
KoinComponent{
override suspend fun doWork(): Result {
val authService:IAuthenticationService by inject()
val token = authService.getAuthToken() // Error here when trying to use it
}
}