0

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

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

当活动进入后台时,我应该使用 fe Bundle 来保存我的数据并立即持久化它们吗?还是应该像在每个片段中一样始终保留一部分数据?还是我只是猎鬼?

4

3 回答 3

3

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

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

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

SQLite 和共享首选项的优缺点

于 2013-08-22T15:38:10.973 回答
2

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

于 2013-08-22T15:37:49.253 回答
0

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

例子:

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

}
于 2018-01-31T06:44:59.787 回答