SharedPreferences.Editor apply() failures

Viewed 113

As docs say,

Unlike commit(), which writes its preferences out to persistent storage synchronously, apply() commits its changes to the in-memory SharedPreferences immediately but starts an asynchronous commit to disk and you won't be notified of any failures.

Does it mean, that in case of some failure (e. g. free space shortage), my SharedPreferences will just revert to previous state, and won't create any exceptions? Will my app crash?

1 Answers

As seen here here, before writing changes to file, SharedPreferences first tries to create a backup file.

If that succeeds:

Attempt to write the file, delete the backup and return true as atomically as possible. If any exception occurs, delete the new file; next time we will restore from the backup.

So, no exceptions will be thrown and SharedPreferences will return to the previous state (because it makes a backup before writing changes to disk). (Although you won't be notified of any failures).

Related