简体   繁体   English

房间迁移失败回退android

[英]room migration failure fallback android

For android's room migrations, are there any methods or techniques that allow a fallback for if for some odd reason (not missing schema - I already know about the fallbackToDestructiveMigration method) a migration fails?对于 android 的房间迁移,是否有任何方法或技术允许回退,如果出于某种奇怪的原因(不缺少模式 - 我已经知道fallbackToDestructiveMigration方法)迁移失败? ie to nuke and recreate the entire db?即核弹并重新创建整个数据库?

Just wrap the whole thing in a try catch block.只需将整个内容包装在一个 try catch 块中。

Source: used it for a migration with 20m+ users.资料来源:将其用于 2000 万以上用户的迁移。 No crashes directly related to the migration没有与迁移直接相关的崩溃

There is a fallbackToDestructiveMigration() method available on the room builder class that is used to generate the database.在用于生成数据库的房间构建器类上有一个可用的fallbackToDestructiveMigration()方法。 ( Room.databaseBuilder ) ( Room.databaseBuilder )

The method explains itself:) It removes the data in Room when the migration fails该方法自行解释:)它会在迁移失败时删除 Room 中的数据

Room.databaseBuilder(getApplicationContext(), MyDatabase.class, "database-name")
    .fallbackToDestructiveMigration()
    .build();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM