【问题标题】:Best practice handling SharedPreferences处理 SharedPreferences 的最佳实践
【发布时间】:2013-08-25 10:35:34
【问题描述】:

我有一个包含一些片段的活动,并且必须保留一些相当数量的数据,但对于 SQL 来说不够公平。现在使用 SharedPreferences 的最佳实践是什么?我想尽可能避免对文件的调用和提交。因为我假设解析该文件,尤其是提交对性能不利。

我知道这个question,它表示对 SharedPreferences 文件的调用总是返回相同的对象。但是提交呢?

我应该使用 f.e.一个Bundle来保存我的数据并在Activity进入后台时立即保存它们?还是应该像在每个片段中一样始终保留一部分数据?还是我只是猎鬼?

【问题讨论】:

  • 共享首选项存在用于存储....首选项,因此不会是海量数据。只是用户偏好(警察大小,清除,缓存,登录,密码,ui颜色......)

标签: android performance sharedpreferences


【解决方案1】:

不确定“相当数量的数据”到底是什么,但请使用 SQL - 这就是它在这里的原因。我真的没有理由不这样做,我知道这真的很容易。如果您从未在 android 上尝试过 sqlite(这可以解释您为什么要尝试避免它:)然后通过基本教程,您就真的完成了。

【讨论】:

  • 我知道 SQL,这不是问题。我只是想知道 SharedPreferences 的性能和正确用法。
  • SharedPreferences 仅存储在 XML 文件中。
【解决方案2】:

我认为这是一种不必要且过早的优化,实际上不会对性能产生任何影响。您在 SharedPreferences 中存储了多少数据?我认为你只是在猎鬼。

如果您将其用作片段之间的通信方式,那么您将其用于非预期目的。

edit:为了进一步评估,SharedPreferences 基本上将内容存储在 Key/Value 映射中。这使得存储和检索简单的东西(例如用户偏好)变得非常方便(因此得名)。如果你需要做比这更复杂的事情,你可以很快看到使用键/值映射会变得多么麻烦,这就是为什么迁移到像 SQLite 这样的数据库存储是有意义的。使用数据库,您可以获得使用查询的明显好处。基本上,SharedPreferences 的重点是为开发人员增加了便利,因此您无需创建完整的数据库来存储简单的值。更多信息请看这里:

Pros and Cons of SQLite and Shared Preferences

【讨论】:

  • 我只是使用提到的 SharedPreferences。对于传递值,我知道更好的方法。但是地图可以像任何其他集合一样增长。关键在于它的性能和正确的用法。
  • @SteveBenett 你看到我的编辑了吗?让我知道如果这不能解决问题。
【解决方案3】:

您可以使用公共类,我们必须在需要时调用它的方法。

示例:

 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;
}

}

【讨论】:

  • 我也创建了相同类型的类,但我使用 Kotlin 扩展函数定义了 getter setter。 arkapp.medium.com/…
猜你喜欢
  • 2014-04-21
  • 2012-02-09
  • 1970-01-01
  • 1970-01-01
  • 2018-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-09-25
相关资源
最近更新 更多