当前位置:网站首页>IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but
IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but
2022-08-02 14:06:00 【慢行的骑兵】
前言
- 使用Room出现的报错,不同场景报错原因可能不同,报错如下:
Room cannot verify the data integrity. Looks like you've changed schema but forgot to update the version number. You can simply fix this by increasing the version number.
结论先行
- 不同业务模块定义的RoomDatabase的实现类,使用了相同的数据库名称,即下方Constants.DB_NAME定义的字符串在不同的RoomDatabase的实现类中使用多次。目前,自己碰到的场景就这一个,不排除其它情况下会产生相同的问题;
Room.databaseBuilder(
context,
ExchangeCourseInfoDataBase.class,
Constants.DB_NAME) //出错的位置,组件化开发的情况下,相同的字符串使用了2次
.allowMainThreadQueries()
.build();
Bug场景复现
- 项目使用的是组件化,两个业务模块都使用了Room做本地数据处理。错误原因是出现在xxxDataBase类中,贴出两个业务模块的代码,对比分析:
模块1的Module1DataBase代码
import cn.xxx.module_exchange_course.mvvm.Constants;
//注解指定了database的表映射实体数据以及版本等信息
@Database(entities = {
ExchangeCourseInfo.class}, version = Constants.DATABASE_VERSION,exportSchema = false)
public abstract class ExchangeCourseInfoDataBase extends RoomDatabase {
private static volatile ExchangeCourseInfoDataBase instance;
public static synchronized ExchangeCourseInfoDataBase getInstance(Context context) {
if (instance == null) {
instance = create(context);
}
return instance;
}
private static ExchangeCourseInfoDataBase create(final Context context) {
return Room.databaseBuilder(
context,
ExchangeCourseInfoDataBase.class,
Constants.DB_NAME) //todo 备注1
.allowMainThreadQueries()
.build();
}
//调换课程信息
public abstract ExchangeCourseInfoDao getExchangeCourseInfoDao();
}
模块2的Module2DataBase代码
//...
import cn.xxx.module_hidden_danger.mvvm.Constants;
//注解指定了database的表映射实体数据以及版本等信息
@Database(entities = {
HiddenDangerInfo.class}, version = Constants.DATABASE_VERSION,exportSchema = false)
public abstract class HiddenDangerInfoDataBase extends RoomDatabase {
private static volatile HiddenDangerInfoDataBase instance;
public static synchronized HiddenDangerInfoDataBase getInstance(Context context) {
if (instance == null) {
instance = create(context);
}
return instance;
}
private static HiddenDangerInfoDataBase create(final Context context) {
return Room.databaseBuilder(
context,
HiddenDangerInfoDataBase.class,
Constants.DB_NAME) //todo 备注2
.allowMainThreadQueries()
.build();
}
//安全隐患信息
public abstract HiddenDangerInfoDao getHiddenDangerInfoDao();
}
- 备注1和备注2位置是导致出现bug的地方,即Room.databaseBuilder方法的第三个参数出现问题导致的bug,具体查看两个业务模块的Constants类。
Module1的Constants类
package cn.xxx.module_exchange_course.mvvm;
public class Constants {
//数据库名称
public static final String DB_NAME = "HiddenDangerDataBase.db";
//数据库版本
public static final int DATABASE_VERSION = 1;
//...
}
Module2的Constants类
package cn.xxx.module_hidden_danger.mvvm;
public class Constants {
//数据库名称
public static final String DB_NAME = "HiddenDangerDataBase.db";
//数据库版本
public static final int DATABASE_VERSION = 1;
//...
}
- 很明显,两个Constants类中的DB_NAME一样,导致的问题。即将Module1和Module2的Constants类中的DB_NAME定义为不同的字符串即可解决问题;
边栏推荐
猜你喜欢
C语言sizeof和strlen的区别
科创知识年度盛会,中国科创者大会8月6日首场开幕!
宝塔搭建DM企业建站系统源码实测
安装使用——百家CMS微商城说明文档(2)
Camera Hal(Hal3)层修改Preview流
C语言一维数组练习——将一个字符串中的某个字符替换成其它字符
MySQL知识总结 (八) InnoDB的MVCC实现机制
Caused by: org.gradle.api.internal.plugins.PluginApplicationException: Failed to apply plugin [id ‘c
YOLOv7 uses cloud GPU to train its own dataset
重新学习编程day1 【初始c语言】【c语言编写出计算两个数之和的代码】
随机推荐
使用flutter小记
线性代数期末复习存档
浮点数的运算方法
华为路由交换
Flask framework in-depth two
C语言日记 6 基本输入/输出
无人驾驶综述:国外国内发展历程
uniapp小程序禁止遮罩弹窗下的页面滚动的完美解决办法
verilog学习|《Verilog数字系统设计教程》夏宇闻 第三版思考题答案(第十四章)
MySQL知识总结 (六) MySQL调优
Kubernetes介绍
C语言sizeof和strlen的区别
static关键字3种作用,简单粗暴对比,好理解
Flink时间和窗口
spark优化
Visual Studio配置OpenCV之后,提示:#include<opencv2/opencv.hpp>无法打开源文件
spark写sql的方式
十分钟带你入门Nodejs
Hession使用
Camera Hal(Hal3)层修改Preview流