我正在使用SharedPreferences來保存Object字段。以下例子:確保正確回寫SharedPreference
class Item {
private String note;
public void setNote(String newNote) {
this.note = newNote;
update();
}
private void update() {
String json = ....; // create JSON image of the object
Editor editor = App.getAppPrefs().edit(); // Load SharedPreferences editor
editor.putString("exampleItem", json);
editor.apply();
}
setNote()(因此update())每次調用「Note」-EditText時被調用。
現在我有幾個問題:如何確認最新版本的「note」已保存?以前的編輯器調用可能會覆蓋最後一次調用嗎?我怎樣才能減少工作量?與爲100個字符文本調用apply()100x相比,必須有一些更智能,更輕的方法。
你如何設置一個標誌,如果用戶沒有輸入超過幾秒鐘然後保存?保存應該幾乎是即時的,但除非您需要保存用戶寫入的每個字符,否則請定期執行。 – Mimminito 2012-03-26 08:07:42
這是一個好主意。謝謝! – damaxxed 2012-03-26 09:05:41