在非活动类别中使用koin时找不到豆

时间:2020-06-04 01:53:43

标签: android koin

我正在尝试使用工作管理器并使用Koin来获取我已设置的一些依赖项。我的工作经理扩展了KoinComponent,这使我可以使用by inject,但是每次尝试使用组件时,我都会尝试得到错误

NoBeanDefFoundException:未找到类AuthenticationService的定义。检查您的定义!

请记住,我在活动和视图模型中都很好地使用了这些依赖项

我的工作经理

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
    }
}

然后在我的Koin模块设置中,我有这个

private val myModule = module {
    single<IAuthenticationService> { AuthenticationService() }
}

我使用了此question作为参考,但是我无法使其正常工作,是我做错了什么主意吗?

2 个答案:

答案 0 :(得分:0)

在Koin中,您应该准确注入您提供的内容。对于您而言,在koin模块中,您提供了一个接口,但在using System.Collections; using System.Collections.Generic; using UnityEngine; public class ARsound : MonoBehaviour { public static AudioClip sound; static AudioSource audioSrc; void Start() { sound = Resources.Load<AudioClip>("AR SFX"); audioSrc = GetComponent<AudioSource>(); } public static void playSound() { audioSrc.PlayOneShot(sound); } } 中,您注入了具体类

我相信您需要注入界面:

BackgroundSync

答案 1 :(得分:0)

NoBeanDefFoundException,意味着不提供对您尝试访问该对象的koin组件的依赖关系。

尝试将实例提供给koin组件,如下所示

private val myModule = module {
    single { AuthenticationService() }
}