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.
- 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?
- 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.
- 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 callsonBackup()
, how will these data changes be handled? Will the BackupManager simply backup the latest version of the database (which should include both changes)? - 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. - 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.
- 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!
From Android Documentation
This should answer your 1 and 2 question :
3: Since your data will be restored during installation i don't think you will face concurrency problems
4:
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