Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

does caching values of SharedPreferences make sense?

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 
}
like image 983
Tobias Avatar asked Nov 08 '11 17:11

Tobias


People also ask

Is SQLite better than SharedPreferences?

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.

Can we store large amount of data in SharedPreferences?

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.

What is the method used to store and retrieve data on the SharedPreferences?

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.

Are SharedPreferences persistent?

Android's built-in SharedPreferences storage mechanism allows us to store information that persists throughout the entire app.


1 Answers

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.

like image 167
Kurtis Nusbaum Avatar answered Nov 11 '22 18:11

Kurtis Nusbaum