我正在使用 koin 1.0.2 来管理依赖项注入,但它不适用于房间数据库,它返回 can't create definition for single ... 这是我的代码:
@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)
}