0
votes

I have a backup activity that user can backup the app database and restore it later... for backup i just copy my app database.db file to sdcard ... for restore i first delete database.db-shm and database.db-wal file if exist (because android 9 use them) and then replace database.db with new one.

restore function works fine in all android ver. backup function also works in all android version, but in android 9.0 its not reliable! because some new data that user has added is in cache files (database.db-shm and database.db-wal) and not applied to database.db

what should i do?

could i forcefully apply cache file to database.db and then backup it?

or ...

1

1 Answers

0
votes

I myself answer...

by help of this link : Temporary Files Used By SQLite

that say : "The WAL file is created when the first connection to the database is opened and is normally removed when the last connection to the database closes. However, if the last connection does not shutdown cleanly, the WAL file will remain in the filesystem and will be automatically cleaned up the next time the database is opened. "

so i close database just before backup and after backup open it again

protected void backupDB() throws IOException {
    SugarContext.terminate(); //Close databse
    String format = new SimpleDateFormat("_yyyyMMdd-HHmmss").format(new Date());
    FileInputStream indb = new FileInputStream(new File(new File(Environment.getDataDirectory() + File.separator + "//data//ir.shia.mohasebe//databases//"), "sugar_example.db"));
    File file = new File(Environment.getExternalStorageDirectory() + File.separator + "Mohasebe Amal");
    file.mkdirs();
    FileOutputStream outdb = new FileOutputStream(new File(file, "mohasebe_backup" + format + ".mbac"));
    FileChannel channel = indb.getChannel();
    FileChannel channel2 = outdb.getChannel();
    channel2.transferFrom(channel, 0, ((FileChannel) channel).size());
    channel.close();
    channel2.close();
    SugarContext.init(getApplicationContext()); //Open database again
}