In my current Android app I have several settings stored in SharedPreferences and one object which handles access to them. I now wonder if it makes sense to cache the values or if doesn't mater much accessing them like:
public final boolean isxxxEnabled() {
return preferences.getBoolean("xxx", false);
}
instead of
public final boolean isxxxEnabled() {
// check if value changed
// if not, check if value is cached
// decide whether to return cached or new
// cache value
return
}
To give an example, SharedPreferences are useful for storing user preferences, where there are just a handful of variables that need storing. SQLite on the other hand would be better for storing data where there is a large set of items, such as song titles in a music library which need to be searched through.
SharedPreferences are not intended to store a lot of data, there is no limit per se (since it is an xml file), but for larger sets of data, I would suggest using Room (or SQLite for the older projects). There is also another reason why storing in a database makes more sense.
Shared Preferences allow you to save and retrieve data in the form of key,value pair. In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.
Android's built-in SharedPreferences storage mechanism allows us to store information that persists throughout the entire app.
Caching shared preferences isn't really necessary. The speed up you're gonna get will be marginal at best and it's going to increase the code you have to write. I'd say don't bother.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With