首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Koin注入contentResolver

如何使用Koin注入contentResolver
EN

Stack Overflow用户
提问于 2020-07-27 16:46:48
回答 1查看 169关注 0票数 0

我正试着在我的数据源类中注入一个带koin的contentProvider,但是我找不到任何方法可以做到这一点。

这是我的dataSource

代码语言:javascript
复制
class MyDataSource(private val application: Application, private val contentProvider: ContentResolver) : MyRepository {...}

和我的koin模块

代码语言:javascript
复制
single<MyRepository> {
        MyDataSource(get(), get())
    }

我得到了这个错误:

原因: org.koin.core.error.NoBeanDefFoundException:找不到'android.content.ContentResolver‘的定义。检查您的模块定义。

EN

回答 1

Stack Overflow用户

发布于 2020-07-27 18:28:19

告诉Koin如何获得ContentResolver。假设您在自定义Application (比方说MyApplication)类中初始化了模块:

代码语言:javascript
复制
private val module = module {

    single { this@MyApplication.contentResolver } // tell Koin this is your ContentResolver

    single<MyRepository> {
        MyDataSource(get(), get()) // now Koin knows how to get the content resolver here
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63111727

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档