拦截房间迁移错误以重新创建数据库

Chr*_*lin 2 android android-room

https://medium.com/google-developers/understanding-migrations-with-room-f01e04b07929 上有一篇关于房间迁移的精彩文章

但是,我仍然想念 Room 中的“灾难”恢复机制(即 fallbackToDestructiveRecreationOnMigrationError() 会在发生奇怪的事情时清除并重新创建数据库)

这是发生在我们身上的事情:

开发人员通过迁移推送了版本 9,但架构 9.json 不符合(我不知道这怎么可能)=> 房间进行了迁移并且没有错误。

然后,当我们提供版本 10 时,文件 9.json 已更改 => 房间崩溃,用户无法再访问该应用程序(这很正常)。

我们不得不告诉我们的用户擦除他们的数据(这不是技术问题,因为我们在应用程序启动时重新同步数据库,但这是一个公共关系问题)

我认为在构建器中使用 openHelperFactory 应该是可能的,但这对我来说还远未深入到房间内部:(

Chr*_*lin 5

经过多次尝试和绝望,这是我使用的解决方案:

abstract class MainDatabase: RoomDatabase() {
    companion object {
        val instance: MainDatabase by lazy {
            if (_instance == null) throw IllegalStateException("someone should have called init fun")
            _instance!!
        }

        private var _instance: MainDatabase? = null
        fun init(mainApplication: MainApplication) {
            _instance = init_(mainApplication)
            //force db opening and if it fails, we try to destroy and recreate the db
            try {
                _instance!!.openHelper.writableDatabase
            } catch (e: Exception) {
                Log.e("Database", "there was an error during DB opening => trying to destroy and recreate", e)
                _instance!!.openHelper.close()
                val dbPath = mainApplication.getDatabasePath(DB_NAME)
                if (SQLiteDatabase.deleteDatabase(dbPath)) {
                    _instance = init_(mainApplication)
                    _instance!!.openHelper.writableDatabase
                }
            }
        }

        private fun init_(mainApplication: MainApplication): MainDatabase {
            return Room.databaseBuilder(mainApplication, MainDatabase::class.java, DB_NAME)
                    .addMigrations(MIGRATION_1, MIGRATION_2, MIGRATION_3, MIGRATION_4, MIGRATION_5, MIGRATION_6, MIGRATION_7, MIGRATION_8, MIGRATION_9, MIGRATION_10)
                    .build()
        }
    }
Run Code Online (Sandbox Code Playgroud)

这个解决方案的真正缺点是对数据库的第一次访问是在主线程上完成的......

如果有人有更好的解决方案,请分享!