Monday, 16 November 2020

createFromAsset and fallbackToDestructiveMigration methods of Room library - no data exists

I have a .db file in assets folder. My RoomDatabase class is as the following. I install the app to my device. Then I changed version = 2 in the below class, and make my prepopulated database version 2. Then i renamed one of the columns in my table so that schema is changed. Then i installed the app again. And boom! Room gives me the following error : 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.

@Database(entities = [Word::class], version = 1, exportSchema = false)
abstract class WordRoomDatabase : RoomDatabase() {

    abstract fun wordDao(): WordDao

    companion object {

        @Volatile
        private var INSTANCE: WordRoomDatabase? = null

        fun getDatabase(context: Context): WordRoomDatabase =
                INSTANCE ?: synchronized(this) {
                    INSTANCE ?: buildDatabase(context).also { INSTANCE = it }
                }

        
        private fun buildDatabase(ctx: Context): WordRoomDatabase {
            val passphrase: ByteArray = SQLiteDatabase.getBytes("my password".toCharArray())
            val factory = SupportFactory(passphrase)

            return Room.databaseBuilder(ctx.applicationContext, WordRoomDatabase::class.java, "word_database2.db")
                    .openHelperFactory(factory)
                    .createFromAsset("word_database.db")
                    .fallbackToDestructiveMigration()
                    .build()
        }
    }
}

After this point, I completely deleted my app. Make the prepopulated database version and @Database version 1. I made allowBackup false in AndroidManifest.xml. So there is no database exist in my device. But I am still facing the same error when I install the app to device. What a nonsense error is this. How can I solve it ?



from createFromAsset and fallbackToDestructiveMigration methods of Room library - no data exists

No comments:

Post a Comment