最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - How to update cached database with assets .db file when it has been changed? - Stack Overflow

programmeradmin2浏览0评论

I'm using Room in Android with a room .db file with preload data in assets. When I compile and install the app in Android Studio emulator with new data (same schema but more or different data on the tables) in the .db preloaded database of assets floder... the app doesn't display it. It seems that room sees that the database has been already loaded from the .db file previously and doesn't update it. Is there any function or configuration in room necessary to always preload the .db file when it has new data? for example, after an update of the app etc...

I don't want to preserve data, I just want that when an update of the app has come, or the .db file has more or different data, the .db file must be loaded, removing/ovewritting the previous cached database. Is there any built in feature in room to help with this?

For example, specifying that the DB version has been changed, and telling Room that it must remove the cached version and reload the assets .db file. But is that possible? how?

this is my database:

@Database(entities = [
    AEntity::class,
    BEntity::class,
], version = 1)
abstract class CustomDatabase : RoomDatabase() {
    abstract fun customDao(): CustomDao
}

and how I load it:

Room.databaseBuilder(
            androidContext(),
            CustomDatabase::class.java, "custom-database"
        )
            .createFromAsset("data.db")
            .build()

What I have tryed?

I tryed increasing version = 1) to 2 and adding .fallbackToDestructiveMigration() before build() in the databaseBuilder, but the result of doing that, is that the app starts with ZERO data, like if the new .db file stored in assets is empty, but is not, it's full of data, I checked it.

发布评论

评论列表(0)

  1. 暂无评论