我正在使用 SharedPreferences 来持久化 Object 字段。举个例子:
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();
}
每次更改“Note”-EditText 时都会调用 setNote()(因此 update())。
现在我有几个问题:如何验证是否保存了最新版本的“note”?最后一次调用是否有可能被之前对编辑器的调用覆盖?我怎样才能最大限度地减少工作量?必须有一些比为 100 个字符的文本调用 apply() 100x 更聪明、更轻松的方法。