首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >RoomDatabase在迁移期间崩溃,即使数据库设置为fallbackToDestructiveMigration

RoomDatabase在迁移期间崩溃,即使数据库设置为fallbackToDestructiveMigration
EN

Stack Overflow用户
提问于 2019-10-05 04:09:21
回答 1查看 1.3K关注 0票数 1

在RoomDatabase迁移过程中,我们看到越来越多的Fabric崩溃。任何解决这个问题的建议

RoomDatabase初始化:

代码语言:javascript
运行
复制
companion object {
        fun createNotesDatabase(androidContext: Context): NotesDatabase {
            try {
                return Room.databaseBuilder(androidContext, NotesDatabase::class.java, "notesdb")
                    .fallbackToDestructiveMigration()
                    .build()
            } catch (e: Exception) {
                Log.e("NotesDatabase", "exception")
            }
        }
    }

异常的堆栈跟踪:

代码语言:javascript
运行
复制
Fatal Exception: java.lang.IllegalStateException: A migration from 1 to 4 was required but not found. Please provide the necessary Migration path via RoomDatabase.Builder.addMigration(Migration ...) or allow for destructive migrations via one of the RoomDatabase.Builder.fallbackToDestructiveMigration* methods.
       at android.arch.persistence.room.RoomOpenHelper.onUpgrade + 97(RoomOpenHelper.java:97)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.onUpgrade + 133(FrameworkSQLiteOpenHelper.java:133)
       at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked + 400(SQLiteOpenHelper.java:400)
       at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase + 298(SQLiteOpenHelper.java:298)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper$OpenHelper.getWritableSupportDatabase + 96(FrameworkSQLiteOpenHelper.java:96)
       at android.arch.persistence.db.framework.FrameworkSQLiteOpenHelper.getWritableDatabase + 54(FrameworkSQLiteOpenHelper.java:54)
       at com.limemobile.notes.data.local.NotesDatabase_Impl.clearAllTables + 280(NotesDatabase_Impl.java:280)
EN

回答 1

Stack Overflow用户

发布于 2019-10-05 04:43:07

你需要做三件事中的一件:-

  1. 提供必要的迁移路径,这需要添加适当的迁移例程或:
  2. 使用通过使用onDestructiveMigration或:-调用的RoomDatabase.Callback方法重写.addCallback(the_CallBack_method),方法:
  3. 将版本更改为1,同时删除数据库(卸载App或删除App的数据将删除数据库)。这样就不会检测到版本更改,因此也就不会期望选项1或2。

哪个选项将取决于未提供的信息,您可能希望阅读用房间理解迁移

附加内容

下面是一个示例,其中onDestructiveMigration方法被重写为什么也不做(数据库表有效地清除了任何数据,但将存在)。

代码语言:javascript
运行
复制
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mAppDB = Room.databaseBuilder(
            this,
            AppDatabase.class,
            "the_database"
    )
            .allowMainThreadQueries()
            .fallbackToDestructiveMigration()
            .addCallback(dbCallback) //<<<<<<<<<<< defines the callback to be called
            .build();
    }
}

//<<<<<<<<<< The callback
private RoomDatabase.Callback dbCallback = new RoomDatabase.Callback() {

    @Override
    public void onCreate(@NonNull SupportSQLiteDatabase db) {
        super.onCreate(db);
        //.......... code here to do anything required when the database is created ..........//
    }

    @Override
    public void onOpen(@NonNull SupportSQLiteDatabase db) {
        super.onOpen(db);
        //.......... code here to do anything required when the database is opened ..........//
    }

    @Override
    public void onDestructiveMigration(@NonNull SupportSQLiteDatabase db) {
        super.onDestructiveMigration(db);
        //.......... code here to do anything required when the database has effectively been recreated (tables would have been built) ..........//
    }
};
  • 不需要覆盖onCreate和onOpen。但是,如果没有适用的迁移,并且数据库是用.fallbackToDestructiveMigration(),构建的,那么onDestructiveMigration方法应该被覆盖(即使什么也不做,这在某些情况下可能是合适的),因为它将被调用。,也就是说,这就是它崩溃的原因
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58245268

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档