IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but

前言

  • 使用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定义为不同的字符串即可解决问题;

你可能感兴趣的:(IllegalStateException: Room cannot verify the data integrity. Looks like you've changed schema but)