Reputation: 262
I am making an app that is able to log in. Each time the user log in, the app will retrieve user data and save it locally so that each time user need to do something which need user information, the app need not to retrieve it again. Is it better to put it in a SharedPreferences or SQLite?
Since it will be always only one user data need to be stored, I think to put it in a SharedPreferences, but it makes my app have so many key-value data. I am also able to use SQLite instead. But, it looks awkward to have a database that always only have one record data in the table.
Is it a good practice to put only one record data in a database and keep replacing it once the data changes? Or is it better to put it in a Shared Preferences and make the SharedPreferences a bit messy because it has many key-value data.
Upvotes: 0
Views: 2759
Reputation: 1483
I would use SharedPreferences for your scenario.
A database would be good if you had several user profiles, but since you only want to store one user profile, it doesn't make sense to create a database for just one row.
Upvotes: 1
Reputation: 1873
You shouldn't use neither SQLite nor SharedPreferences, the thing is, android done it for you, you should something that's called AccountManager
Why you should it: This class provides access to a centralized registry of the user's online accounts. The user enters credentials (username and password) once per account, granting applications access to online resources with "one-click" approval.
You can read about this class here : http://developer.android.com/reference/android/accounts/AccountManager.html
But what you should know is that you can also remove accounts there, so it's especially good for you, here is tutorial Step by step: http://www.finalconcept.com.au/article/view/android-account-manager-step-by-step-2
Upvotes: 0
Reputation: 1269
It totally depend on your data, which you want to store. I would personally recommend you to store in sharedpreferences. SQLite is used to store large, Structured and Organized data where as sharedpreferences are used to store small, unstructured data like login info, user preferences etc
Upvotes: 1
Reputation: 305
I think SharedPreferences is best way to store profile data.... In SharedPreferences also we can replace stored data.
public class SharedPref {
private SharedPreferences pref;
private Editor editor;
private Context context;
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREFER_NAME = "SharedPreferences";
// All Shared Preferences Keys
private static final String IS_USER_LOGIN = "IsUserLoggedIn";
@SuppressLint("CommitPrefEdits")
public SharedPref(Context context){
this.context = context;
this.pref = context.getSharedPreferences(PREFER_NAME, PRIVATE_MODE);
this.editor = pref.edit();
}
//Set current tab position
public void setCurrentTab(String currentTab) {
editor.putString(Constant.TabActivity.CURRENT_TAB,currentTab);
editor.commit();
}
public void setTabActivityTitle(String title) {
editor.putString(Constant.TabActivity.TAB_ACTIVITY_TITLE,title);
editor.commit();
}
public String getSessionValue(String key) {
return pref.getString(key, null);
}
}
Upvotes: 1