-
-
Notifications
You must be signed in to change notification settings - Fork 507
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Android] Replace SharedPreferences
with DataStore
Preferences
#629
base: master
Are you sure you want to change the base?
Conversation
@oblador Any idea why the error about Could it be a side-effect of adding Kotlin?! 🤷🏻 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would be a good addition to the library
@@ -10,6 +10,8 @@ buildscript { | |||
set("targetSdkVersion", 31) | |||
set("buildToolsVersion", "31.0.0") | |||
} | |||
val kotlinVersion by extra("1.8.0") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If the root project uses kotlin this should use that version instead, ex my project is using 1.9.22
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