Android SQLite "no such table: room_table_modification_log" after restore

146 Views Asked by At

I am trying to implement SQLite database based on SQLCipher (https://github.com/sqlcipher/android-database-sqlcipher) but after I restore the database and try to perform any actions on it I get the following error:

Cannot run invalidation tracker. Is the db closed?

android.database.sqlite.SQLiteException: no such table: room_table_modification_log: , 
while compiling: SELECT * FROM room_table_modification_log WHERE invalidated = 1;

If I close the app from recent apps and open the app again, no error is thrown.

I initialize my database using:

fun initialize(passphrase: String): Boolean {
    this.passphrase = SQLiteDatabase.getBytes(passphrase.toCharArray())
    this.factory = SupportFactory(this.passphrase, null, false)

    return try {
        db = Room.databaseBuilder(
            context.applicationContext,
            MyDatabase::class.java,
            DATABASE_NAME
        )
            .openHelperFactory(factory)
            .build()

        db.query("SELECT 1", emptyArray())

        true
    } catch (e: Exception) {
        false
    }
}

Then when user presses to restore the database, he selects the file and then the following method is called to restore the database:

fun restoreDatabase(backupFilePath: Uri, passphrase: String = "a"): Boolean {
    return try {
        db.close()

        val dbFile = context.getDatabasePath(DATABASE_NAME)
        val file = createTempDatabaseFile(backupFilePath) ?: return false
        file.copyTo(dbFile, overwrite = true)

        return initialize("a")
    } catch (e: Exception) {
        e.printStackTrace()
        false
    }
}

P.S:

  1. I tried not closing the database, but this way the content does not change.
  2. The error is not thrown only when I close the app from recents and reopen it again. Restarting the activity does not help:
val intent = Intent(context, MainActivity::class.java)
(context as MainActivity).finish()
context.startActivity(intent)

Library versions I use as of now:

room = "2.6.0-alpha01"

sqlcipher = "4.5.4"

0

There are 0 best solutions below