简体   繁体   English

Android SharedPreferences在多个进程之间不一致

[英]Android SharedPreferences not consistent between multiple processes

I'm having troubles trying to get SharedPreferences working. 我在尝试使SharedPreferences工作时遇到麻烦。

this is the code: 这是代码:

/**
 * Sets the software in synchronizing status.
 * @param synchronizing Boolean
 */
public void setSynchronizing(boolean synchronizing) {
    if(D) Log.d(TAG, "Called: setSynchronizing("+synchronizing+")");
    SharedPreferences preferences = mContext.getSharedPreferences(SharedPrefsConstants.PREFERENCES, 0);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean(SharedPrefsConstants.SYNCHRONIZING, synchronizing);
    boolean result = editor.commit();
    if(!result)
        Log.w(TAG, "Cannot store the preference.");
    if(!synchronizing)
        BroadcastUtils.stopSynchronizing(mContext);
}

/**
 * Returns whether the software is synchronizing.
 * @return True if synchronization is happening.
 */
public boolean isSynchronizing() {
    SharedPreferences preferences = mContext.getSharedPreferences(SharedPrefsConstants.PREFERENCES, 0);
    boolean synchronizing = preferences.getBoolean(SharedPrefsConstants.SYNCHRONIZING, false);
    if(D) Log.d(TAG, "Called: isSynchronizing Returning: "+synchronizing);
    return synchronizing;
}

and this is the logcat output, please note that i'm using two separate processes in my application, i'll call them app and app:bg : 这是logcat的输出,请注意,我在应用程序中使用了两个单独的进程,我将它们分别称为appapp:bg

**app** D/StorageManager﹕ Called: setSynchronizing(true)
**app** D/StorageManager﹕ Called: setSynchronizing(true)
**app** D/StorageManager﹕ Called: isSynchronizing Returning: true
**app** D/StorageManager﹕ Called: isSynchronizing Returning: true
**app:bg** D/StorageManager﹕ Called: setSynchronizing(false)
**app** D/StorageManager﹕ Called: isSynchronizing Returning: true

StorageManager is a singleton instance, but there are two of those instances, one for each process. StorageManager是一个单例实例,但其中有两个实例,每个实例一个。

Even if setSynchronizing(false) is being called from background thread, the physical preference file is changed correctly, but in the foreground thread it is still true. 即使从后台线程调用setSynchronizing(false),物理首选项文件也已正确更改,但是在前台线程中它仍然为true。

You can see that isSynchronizing method is returning true after setSynchronizing sets that variable to false. 您可以看到在setSynchronizing将变量设置为false之后,isSynchronizing方法返回true。 Question is: why do this happens? 问题是: 为什么会这样? This is the first time i use SharedPreferences in this software, so it cannot be set anywhere else. 这是我第一次在此软件中使用SharedPreferences,因此无法在其他任何地方进行设置。

This is the preference file stored inside the phone, when isSynchronizing is still returning TRUE: 当isSynchronizing仍返回TRUE时,这是手机中存储的首选项文件:

<?xml version='1.0' encoding='utf-8' standalone='yes' ?>
<map>
<boolean name="synchronizing" value="false" />
</map>

The only thing that i can think of is that SharedPreferences holds some sort of cache in memory, if you can confirm this there is some way to force the update of a SharedPreference? 我唯一能想到的是,SharedPreferences在内存中拥有某种缓存,如果您可以确认这一点,则可以通过某种方式强制更新SharedPreference?

I must also say that a considerable amount of time is passing between the variable being set to false and any other subsequent call to isSynchronizing from the foreground thread. 我还必须说,在将变量设置为false以及从前台线程对isSynchronizing进行的任何其他后续调用之间,要花费大量的时间。

I found a solution. 我找到了解决方案。

It seems like SharedPreferences is not a Process-Safe class, it can work with multiple processes if you set a flag when calling getSharedPreferences: 似乎SharedPreferences不是一个进程安全的类,如果在调用getSharedPreferences时设置一个标志,它可以与多个进程一起使用:

MODE_MULTI_PROCESS

This solves the problem. 这样就解决了问题。

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

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