简体   繁体   English

SharedPreferences - Android

[英]SharedPreferences - Android

I have a little doubt about the SharedPreferences in Android . 我对AndroidSharedPreferences有一点怀疑。

To delete a preference, we mainly have two options: 要删除首选项,我们主要有两个选项:

First: 第一:

SharedPreferences.Editor edit = (Editor) getSharedPreferences(Constants.APP_DEFAULT_PREF, MODE_PRIVATE).edit();
edit.putString(Constants.PREF_ACC, null);
edit.commit();

Second: 第二:

SharedPreferences.Editor edit = (Editor) getSharedPreferences(Constants.APP_DEFAULT_PREF, MODE_PRIVATE).edit();
edit.remove(Constants.PREF_ACC);
edit.commit();

In either case, fetching Constants.PREF_ACC value from SharedPreferences will return null . 在任何一种情况下,从SharedPreferences获取Constants.PREF_ACC值都将返回null

I was wondering which one should I prefer. 我想知道我应该选择哪一个。 Is there any memory related issues in either of them? 其中任何一个都有任何与内存相关的问题吗? What will the System.gc behavior be with them? System.gc行为与他们有什么关系?

Theoretically remove is better than put(null) , because it removes both the key and value (once committed) instead of mapping (and keeping) the key to a null value. 理论上removeput(null)更好,因为它删除了键和值(一旦提交),而不是将键映射(并保持)为空值。

But judging by the Android 5.1.1 implementation , they are equivalent : 但从Android 5.1.1实现来看,它们是等价的:

    ...
    String k = e.getKey();
    Object v = e.getValue();
    // "this" is the magic value for a removal mutation. In addition,
    // setting a value to "null" for a given key is specified to be
    // equivalent to calling remove on that key.
    if (v == this || v == null) {
        if (!mMap.containsKey(k)) {
            continue;
        }
        mMap.remove(k);
    } else {
    ...

That is also what one of the putXXX methods ( putStringSet ) documentation says : 这也是的一个什么putXXX方法( putStringSet )文档说:

Passing null for this argument is equivalent to calling remove(String) with this key. 为此参数传递null等效于使用此键调用remove(String)。

I would recommend using remove . 我建议使用删除

When we putString or remove nothing is done, it is just marked in the Editor as TO BE DONE and gets done only when commit is called And when the commit is called all the remove calls are executed before the put calls. 当我们putString删除没有做任何事情时,它只是在编辑器中标记为TO BE DONE并且仅在调用commit时才完成。当调用commit时,所有remove调用都在put调用之前执行。 So it is better to use the remove calls to remove something from the Editor. 所以最好使用remove调用从编辑器中删除一些东西。

Judging by the docs of the interface SharedPreferences.Editor for remove(String) : 通过用于remove(String)的接口SharedPreferences.Editordocs来判断:

Mark in the editor that a preference value should be removed, which will be done in the actual preferences once commit() is called. 在编辑器中标记应该删除首选项值,这将在调用commit()后在实际首选项中完成。

Note that when committing back to the preferences, all removals are done first, regardless of whether you called remove before or after put methods on this editor. 请注意,在提交回首选项时,无论是否在此编辑器上调用put方法之前或之后调用remove,都会先执行所有删除操作。

… and for putInt(int) : ...和putInt(int)

Set an int value in the preferences editor, to be written back once commit() or apply() are called. 在首选项编辑器中设置一个int值,一旦调用commit()或apply()就会被写回。

… there seem to be only one striking difference: remove(String) calls will be "done first, regardless of whether you called remove before or after put methods" . ...似乎只有一个显着的区别: remove(String)调用将“首先完成,无论你是否在put方法之前或之后调用remove”

That said, I really doubt the actual order of execution won't matter much to average use-cases, so you could just choose either one of those methods and be completely fine. 也就是说,我真的怀疑实际的执行顺序对平均用例并不重要,所以你可以选择其中一种方法并完全没问题。

ps , I'm still looking for the concrete class of SharedPreferences.Editor which might provide more clues about this. ps ,我仍然在寻找具体的SharedPreferences.Editor类,它可能提供更多关于此的线索。 Will update as soon as I found one. 我会在找到一个后立即更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM