gpt4 book ai didi

android - Dagger 2 获得自己的 Room 实例

转载 作者:行者123 更新时间:2023-12-02 12:13:38 25 4
gpt4 key购买 nike

我想向房间数据库添加回调以填充初始数据。

@Provides
@Singleton
fun provideRoom(context: Context): MyRoomDatabase {
return Room.databaseBuilder(context, MyRoomDatabase::class.java, "my_database")
.fallbackToDestructiveMigration()
.addCallback(object : RoomDatabase.Callback() {
@Override
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)

}
})
.build()
}

为此,我需要回调中的数据库实例来访问 DAO 以插入数据。
这是如何运作的?

编辑:

我要达到的目标: Create initial data for the room database at the app installation
我的 Callback Class :
class RoomCallback(
var myRoomDatabase : MyRoomDatabase
) : RoomDatabase.Callback() {
override fun onCreate(db: SupportSQLiteDatabase) {
myRoomDatabase.basicItemDao().insertList(
listOf(
BasicItem(),
BasicItem()
)
)
}

}

我如何提供 RoomDatabaseRoomCallback :
@Provides
@Singleton
fun provideRoom(context: Context, roomCallback: RoomCallback): MyRoomDatabase {
return Room.databaseBuilder(context, MyRoomDatabase::class.java, "my_database")
.fallbackToDestructiveMigration()
.addCallback(roomCallback)
.build()
}

@Provides
@Singleton
fun provideRoomCallback(myRoomDatabase: MyRoomDatabase): RoomCallback {
return RoomCallback(myRoomDatabase)
}

问题:
- RoomCallbackRoomDatabase实例需要另一个实例。

最佳答案

更新 : 使用 Kotlin CoroutineDagger2在聚会的后期,但对于 future 的读者来说,在创建时或打开时预填充您的数据库非常容易。确保您已经在 gradle 中添加了依赖项Coroutine 的文件.首先创建您的数据库,如:

    /**
* Main database.
*/
@Database(
entities = [
Login::class],
version = 1,
exportSchema = false
)
abstract class AppDatabase : RoomDatabase() {
abstract fun loginDao(): LoginDao

companion object {

@Volatile private var INSTANCE: AppDatabase? = null

fun getInstance(app: Application): AppDatabase = INSTANCE ?: synchronized(this) {
INSTANCE ?: buildDatabase(app).also { INSTANCE = it }
}

private fun buildDatabase(app: Application) =
Room.databaseBuilder(app,
AppDatabase::class.java,
"your database name")
.addCallback(object : Callback() {
// Pre-populate the database after onCreate has been called. If you want to prepopulate at opening time then override onOpen function instead of onCreate.
override fun onCreate(db: SupportSQLiteDatabase) {
super.onCreate(db)
// Do database operations through coroutine or any background thread
val handler = CoroutineExceptionHandler { _, exception ->
println("Caught during database creation --> $exception")
}

CoroutineScope(Dispatchers.IO).launch(handler) {
prePopulateAppDatabase(getInstance(app).loginDao())
}
}
})
.build()

suspend fun prePopulateAppDatabase(loginDao: LoginDao) {
val admin = Login(0, "Administrator", "1234", 1, isActive = true, isAdmin = true, isLogged = false)
loginDao.insertLoginData(admin)
}
}
}
然后,您可以通过将以下代码放入您的 Dagger AppModule 来提供单例实例或根据需要在单独的数据库模块中。
@Singleton
@Provides
fun provideDb(app: Application): AppDatabase {
return AppDatabase.getInstance(app)
}

@Singleton
@Provides
fun provideLoginDao(db: AppDatabase): LoginDao {
return db.loginDao()
}
就是这样,你完成了。在任何地方注入(inject)您的单例数据库对象,例如:
@Inject lateinit var loginDao: LoginDao
然后使用它。

关于android - Dagger 2 获得自己的 Room 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57968242/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com