[英]Kotlin 1.0.2 Room Database
I'm using koin 1.0.2 to manage dependencies injection but it's not working with room database, it's returning can't create definition for single... Here is my code: 我正在使用koin 1.0.2来管理依赖项注入,但是它不能与会议室数据库一起使用,返回时无法为单个对象创建定义。这是我的代码:
@Database(entities = [UserEntity::class], version = 1, exportSchema = false)
abstract class UserDatabase : RoomDatabase() {
abstract fun userDao(): UserDao
companion object {
private var INSTANCE: UserDatabase? = null
fun getInstance(context: Context): UserDatabase {
if (INSTANCE == null) {
INSTANCE = Room.databaseBuilder(
context,
UserDatabase::class.java,
"user_database")
.allowMainThreadQueries()
.build()
}
return INSTANCE!!
}
}
}
val DatabaseModule = module {
single { UserDatabase.getInstance( get())}
}
class UserApplication : Application() {
override fun onCreate() {
super.onCreate()
startKoin(getModules())
}
private fun getModules() = listOf(DatabaseModule, RepositoryModule, ViewModule)
}
I have tried your code in a test project with sample entities and it worked. 我已经在带有示例实体的测试项目中尝试了您的代码,并且该代码有效。 Since you haven't posted the actual error description, please do/ensure the following steps and try again.
由于您尚未发布实际的错误说明,因此请执行/确保以下步骤,然后重试。
Add the following dependencies to build.gradle
将以下依赖项添加到
build.gradle
implementation "androidx.room:room-runtime:$room_version"
kapt "androidx.room:room-compiler:$room_version"
If you are using RxJava for room, then add the following line as well 如果您正在使用RxJava作为空间,那么也请添加以下行
implementation "androidx.room:room-rxjava2:$room_version"
Apply the kotlin-kapt
plugin 应用
kotlin-kapt
插件
apply plugin: 'kotlin-kapt'
clean and build. 清洁和建造。
If you are still experiencing the problem, consider editing the question with logcat stack trace. 如果仍然遇到问题,请考虑使用logcat堆栈跟踪来编辑问题。
Have you declared a definition for a Context
object in other modules? 您是否在其他模块中声明了
Context
对象的定义? Because your database method needs this one and you are calling get()
to resolve it. 因为您的数据库方法需要这一方法,所以您正在调用
get()
来解决它。 If not, just change 如果没有,那就换
single { UserDatabase.getInstance(get()) }
to 至
single { UserDatabase.getInstance(androidContext()) }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.