[Android] Replace SharedPreferences
with DataStore
Preferences
#629
+250
−180
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Closes #628
This PR replaces the usage of
SharedPreferences
withDataStore
.Why
Google is recommending in their official documentation to migrate to
DataStore
fromSharedPreferences
, and they also provided a very easy-to-use auto-migration tool that will move the data to the newDataStore
, then clean up the entries fromSharedPreferences
.Their official guide for saving simple data with SharedPreferences displays an alert about this:
For security concerns as well, it is recommended to migrate to DataStore.
How
Since
DataStore
APIs are tailored for Kotlin, the code for using it is also done in Kotlin, thus introducing Kotlin to the project.Calls to read and write to DataStore are forced to run synchronously based on the example provided by the Android team in Use DataStore in synchronous code.
Data Migration
SharedPreferencesMigration
is used to migrate the entries stored in theRN_KEYCHAIN
shared prefs file to data store. Upon completion, theSharedPreferences
get removed.Testing
There's an unit test added to check this, and I also tested it and it proved to work seamlessly in a React-Native app that is using this library, resulting in no data loss for the user or any issues when this gets upgraded.
Result
SharedPreferences
DataStore