Android 处理SharedReference的最佳实践
我有一个带有一些片段的活动,必须保留一些相当数量的数据,但对于SQL来说不够公平。现在,使用SharedReference的最佳实践是什么?我希望尽可能避免对文件的调用和提交。因为我假设解析该文件,尤其是提交对性能有害 我知道这一点,即对SharedReferences文件的调用总是返回相同的对象。但那承诺呢Android 处理SharedReference的最佳实践,android,performance,sharedpreferences,Android,Performance,Sharedpreferences,我有一个带有一些片段的活动,必须保留一些相当数量的数据,但对于SQL来说不够公平。现在,使用SharedReference的最佳实践是什么?我希望尽可能避免对文件的调用和提交。因为我假设解析该文件,尤其是提交对性能有害 我知道这一点,即对SharedReferences文件的调用总是返回相同的对象。但那承诺呢 我是否应该使用f.e.捆绑包来保存我的数据,并在活动进入后台时立即保存它们?或者我应该像在每个片段中一样始终保留一部分数据吗?或者我只是在寻找鬼魂?不确定“公平数量的数据”到底是什么,但请
我是否应该使用f.e.捆绑包来保存我的数据,并在活动进入后台时立即保存它们?或者我应该像在每个片段中一样始终保留一部分数据吗?或者我只是在寻找鬼魂?不确定“公平数量的数据”到底是什么,但请使用SQL——这就是为什么它会出现在这里。我真的这么做了,没有理由不这么做,因为我知道这是多么容易。如果您从未在android上尝试过sqlite(这可以解释为什么您想要尝试避免它:),那么请阅读基础教程,您就真的完成了。不确定“公平数据量”到底是什么,但请使用SQL-这就是为什么它在这里的原因。我真的这么做了,没有理由不这么做,因为我知道这是多么容易。如果您从未在android上尝试过sqlite(这可以解释为什么您想要尝试避免它:),那么通过初级教程,您就真的完成了。我认为这是一个不必要的过早优化,实际上不会对性能产生任何影响。您在SharedReferences中存储了多少数据?我想你只是在打鬼 如果您将其用作片段之间的通信手段,那么您将其用于非预期目的 编辑:为了进一步评估,SharedReferences基本上将内容存储在键/值映射中。这使得存储和检索诸如用户首选项(因此得名)之类的简单内容变得非常方便。如果您需要做比这更复杂的事情,您可以很快看到使用键/值映射会变得多么麻烦,这就是为什么移动到像SQLite这样的数据库存储是有意义的。使用数据库,您可以从使用查询中获得明显的好处。基本上,SharedReferences为开发人员提供了便利,因此您不需要创建完整的数据库来存储简单的值。请参见此处了解更多信息:
我认为这是一个不必要的、过早的优化,实际上不会对性能产生任何影响。您在SharedReferences中存储了多少数据?我想你只是在打鬼 如果您将其用作片段之间的通信手段,那么您将其用于非预期目的 编辑:为了进一步评估,SharedReferences基本上将内容存储在键/值映射中。这使得存储和检索诸如用户首选项(因此得名)之类的简单内容变得非常方便。如果您需要做比这更复杂的事情,您可以很快看到使用键/值映射会变得多么麻烦,这就是为什么移动到像SQLite这样的数据库存储是有意义的。使用数据库,您可以从使用查询中获得明显的好处。基本上,SharedReferences为开发人员提供了便利,因此您不需要创建完整的数据库来存储简单的值。请参见此处了解更多信息:
您可以使用公共类,我们必须在需要时调用它的方法 例如:
SessionManager mSessionManager = new SessionManager(this);
mSessionManager.putStringData("key", "value");
课程内容如下:
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
public class SessionManager {
// Shared Preferences
SharedPreferences pref;
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int MODE_MULTI_PROCESS = 0;
// Sharedpref file name
private static final String PREF_NAME = "SharedPref_Name";
private SharedPreferences getPref() {
return _context.getSharedPreferences(PREF_NAME, Context.MODE_MULTI_PROCESS);
}
// Constructor
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, MODE_MULTI_PROCESS);
editor = pref.edit();
}
/**
* Set the String data in the preferences.
*/
public void putStringData(String keyname, String value) {
editor.putString(keyname, value);
editor.commit();
}
/**
* @return the string data from the prefs
*/
public String getStringData(String keyName) {
return pref.getString(keyName, "");
}
/**
* Set the int data in the preferences.
*/
public void putIntData(String keyname, int value) {
editor.putInt(keyname, value);
editor.commit();
}
/**
* @return the boolean data from the prefs
*/
public int getIntData(String keyName) {
return pref.getInt(keyName, 0);
}
/**
* Set the boolean data in the preferences.
*/
public void putBooleanData(String keyname, boolean value) {
editor.putBoolean(keyname, value);
editor.commit();
}
/**
* @return the boolean data from the prefs
*/
public boolean getBooleanData(String keyName) {
return pref.getBoolean(keyName, false);
}
/**
* Set the long data in the preferences.
*/
public void putLongData(String keyname, long value) {
editor.putLong(keyname, value);
editor.commit();
}
/**
* @return the long data from the prefs
*/
public long getLongData(String keyName) {
return pref.getLong(keyName, 99);
}
/**
* remove data from pref
*
* @param keyName
*/
public void removeData(String keyName) {
editor.remove(keyName);
editor.commit();
}
//Save arrayList of Model type
public void saveAssignedLocationsToSharedPrefs(List<Locations> LocationModel) {
Gson gson = new Gson();
String jsonLocation = gson.toJson(LocationModel);
editor.putString("LocationArray", jsonLocation);
editor.commit();
}
//get arrayList of Model type
public ArrayList<Locations> getAssignedLocationsFromSharedPrefs() {
List<Locations> LocationData;
String jsonLocation = pref.getString("LocationArray", null);
Gson gson = new Gson();
Locations[] LocationItems = gson.fromJson(jsonLocation,
Locations[].class);
LocationData = Arrays.asList(LocationItems);
LocationData = new ArrayList<Locations>(LocationData);
return (ArrayList<Locations>) LocationData;
}
}
导入android.content.Context;
导入android.content.SharedReferences;
导入android.content.SharedReferences.Editor;
公共类会话管理器{
//共享首选项
共享参考优先;
//共享首选项编辑器
编辑;
//上下文
语境(Context)语境;;
//共享优先模式
int MODE_MULTI_进程=0;
//Sharedpref文件名
私有静态最终字符串PREF_NAME=“SharedPref_NAME”;
私有SharedReferences getPref(){
返回_context.getSharedReferences(PREF_名称、context.MODE_多进程);
}
//建造师
公共会话管理器(上下文){
这._context=context;
pref=\u context.getSharedReferences(pref\u名称、模式\u多进程);
编辑器=pref.edit();
}
/**
*在首选项中设置字符串数据。
*/
public void putStringData(字符串键名、字符串值){
编辑器.putString(键名、值);
commit();
}
/**
*@从prefs返回字符串数据
*/
公共字符串getStringData(字符串键名){
返回pref.getString(keyName,“”);
}
/**
*在首选项中设置int数据。
*/
public void putindata(字符串键名,int值){
编辑器.putInt(键名、值);
commit();
}
/**
*@从prefs返回布尔值数据
*/
public int getIntData(字符串键名){
返回pref.getInt(keyName,0);
}
/**
*在首选项中设置布尔数据。
*/
public void putBooleanData(字符串键名,布尔值){
编辑器.putBoolean(键名、值);
commit();
}
/**
*@从prefs返回布尔值数据
*/
公共布尔getBooleanData(字符串键名){
返回pref.getBoolean(keyName,false);
}
/**
*在首选项中设置长数据。
*/
public void putLongData(字符串键名,长值){
编辑器.putLong(键名、值);
commit();
}
/**
*@从prefs返回长数据
*/
公共长getLongData(字符串键名){
返回pref.getLong(keyName,99);
}
/**
*从pref中删除数据
*
*@param-keyName
*/
public void removeData(字符串键名){
editor.remove(keyName);
commit();
}
//保存模型类型的arrayList
public void saveAssignedLocationsToSharedPrefs(列表位置模型){
Gson Gson=新的Gson();
字符串jsonLocation=gson.toJson(LocationModel);
putString(“LocationArray”,jsonLocation);
commit();
}
//获取模型类型的arrayList
公共阵列列表getAssignedLocationsFromSharedPrefs(){
列出位置数据;
字符串jsonLocation=pref.getString(“LocationArray”,null);
G