我正在使用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