Questions about using Android Backup Service with a SQLite database

550 views Asked by At

My app stores all user data and preferences in a SQLite database, which I'd like to persist if the user gets a new phone, reinstalls, or does a factory reset. I've done some reading in Android's Data Backup Guide and their Android Backup Service, but still have some questions before I get started.

  1. Is the data restored during install or on initial launch of my app? i.e., will the file exist before the code for my main Activity is called?
  2. If my SQLiteOpenHelper class already handles upgrades, is there anything else I have to do to handle restoration of an older db? I assume not, if the db exists before my app is launched.
  3. Will I face concurrency issues when backing up because the BackupManager takes time? If a user enters new data and I call dataChanged() and then the same happens again before the BackupManager calls onBackup(), how will these data changes be handled? Will the BackupManager simply backup the latest version of the database (which should include both changes)?
  4. What problems will I face by trying to backup/restore a SQLite database? I've seen developers say that a database on one device may not be compatible with a database on another. I'd like to simply backup the database using FileBackupHelper, because it looks pretty simple to do. At least one developer seems to have resolved at least some compatibility issues by disabling Write Ahead Logging. If I take this approach, what rate of failure might I expect? If very high, I might look into converting to a CSV file and back during the process.
  5. If a restore fails, can I catch it, inform the user, and save a copy of the troublesome data to storage? For troubleshooting and informing the user.
  6. The backup operation is "not threadsafe." Clarify how to handle. Android says,

To ensure that your backup agent does not read or write your files at the same time as your activities, you must use synchronized statements each time you perform a read or write.

  • Does this mean that I must put every read/write to my database within a synchronized statement? i.e., I have to go add this to every place my Activities load from the db or write info to it? That's a lot of places.
  • Or does it mean only the Backup code must use synchronized?

Thanks for the help. I just want to do this right and keep my users happy when they get new phones this Christmas!

1

There are 1 answers

0
MineConsulting SRL On

From Android Documentation

This should answer your 1 and 2 question :

Android automatically performs a restore operation when your application is installed and there 
exists backup data associated with the user. The primary scenario in which backup data is restored 
is when a user resets their device or upgrades to a new device and their previously installed 
applications are re-installed.

3: Since your data will be restored during installation i don't think you will face concurrency problems

4:

Data backup is not guaranteed to be available on all Android-powered devices. However, your   
application is not adversely affected in the event that a device does not provide a backup 
transport. If you believe that users will benefit from data backup in your application, then you can 
implement it as described in this document, test it, then publish your application without any 
concern about which devices actually perform backup.

5: I don't think so.

6: "each time you perform a read or write" seems to be self-explanatory. Every time you you do an operation on the DB