i'm learn how to use Room from Codelabs and now i have two table
when i'm run from Android Studio is normal
but when i'm close and re open the app i got error
java.lang.IllegalStateException: 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.
error when re open the app, not on install
i'm try to up version number and i still got the error
here my code
@Database(entities = [Type::class], version = 3)
abstract class TypeRoomDb : RoomDatabase(){
abstract fun typeDao() : TypeDao
companion object{
@Volatile
private var INSTANCE : TypeRoomDb? = null
fun getDataBase(
context: Context,
scope: CoroutineScope
): TypeRoomDb {
return INSTANCE ?: synchronized(this){
val instance = Room.databaseBuilder(
context.applicationContext,
TypeRoomDb::class.java,
Cons.DB_NAME
)
.fallbackToDestructiveMigration()
.addCallback(TypeDbCallBack(scope))
.build()
INSTANCE = instance
instance
}
}
private class TypeDbCallBack(
private val scope: CoroutineScope
) : RoomDatabase.Callback(){
override fun onOpen(db: SupportSQLiteDatabase) {
super.onOpen(db)
INSTANCE?.let { database ->
scope.launch(Dispatchers.IO) {
populateDb(
database.typeDao()
)
}
}
}
}
fun populateDb(typeDao: TypeDao){
typeDao.deleteAll()
/*out*/
typeDao.insert(
Type(
"000",
"Makan",
0
)
)
typeDao.insert(
Type(
"001",
"Transportasi",
0
)
)
typeDao.insert(
Type(
"002",
"Makanan Ringan",
0
)
)
typeDao.insert(
Type(
"003",
"Komunikasi",
0
)
)
/*in*/
typeDao.insert(Type(
"500",
"Gaji",
1))
typeDao.insert(
Type(
"5001",
"Hadiah",
1
)
)
}
}
}
my second table
@Database(entities = [LogKeuangan::class], version = 2)
abstract class LogKeuanganRoomDb : RoomDatabase() {
abstract fun logKeuanganDao(): LogKeuanganDao
companion object {
@Volatile
private var INSTANCE: LogKeuanganRoomDb? = null
fun getDataBase(
context: Context,
scope: CoroutineScope
): LogKeuanganRoomDb {
return INSTANCE ?: synchronized(this) {
val instance = Room.databaseBuilder(
context.applicationContext,
LogKeuanganRoomDb::class.java,
Cons.DB_NAME
)
.fallbackToDestructiveMigration()
.build()
INSTANCE = instance
instance
}
}
}
}