何时调用 editor.apply?

Wra*_*ous 1 java android sharedpreferences

在我的应用程序中,我最多可以由用户定义 20 个假期,当将它们保存到共享首选项编辑器时,我将其设置如下:

String checkhold = "0";
String holodate = g.getData82();// string is either "0" if no holidays entered, or, for example, "1,1,7,4,12,25, if January 1st, July 4th and December 25th is entered

if (!holodate.equals("0")) {

    checkhold = holodate;
    int value = 0;
    String splitHere = "[,]";
    String[] sToken = holodate.split(splitHere);
    int count = holodate.length() - checkhold.replace(",", "").length();
    g.setData92(emp, count);
    double holidayNumber = count * 0.5;
    passeditor.putDouble(holnums, holidayNumber);

if (count >= 0) {
    value = Integer.parseInt(sToken[0]);
    g.setData90(0, value);
    passeditor.putInt(holmonth1, value);passeditor.apply();
    value = Integer.parseInt(sToken[1]);
    g.setData91(0, value);
    passeditor.putInt(holday1, value);passeditor.apply();
    }

    if (count >= 2) {
    value = Integer.parseInt(sToken[2]);
    g.setData90(1, value);
    passeditor.putInt(holmonth2, value);passeditor.apply();
    value = Integer.parseInt(sToken[3]);
    passeditor.putInt(holday2, value);passeditor.apply();
    g.setData91(1, value);
    }
//....continue 18 more code blocks
}
Run Code Online (Sandbox Code Playgroud)

正如你所看到的,我已经调用了 passitor.apply 40 次,但我想知道的是,在这 20 个代码块的情况下,我是否需要在每次 passitor.put 之后调用 passitor.apply 或者我可以在每个代码块的末尾,或者我可以在所有 20 个代码块的末尾调用一次吗?只是想消除不必要的代码

Tom*_*ura 5

当您想要保存更改时调用它SharedPreferences- 您可以在编辑结束时调用它一次。