Invalid SharedPreference access value in different threads

In my application, after logging in, I save user data, such as (username, identifier, email, etc.) in a sharedPreference file so that I can access them anywhere in the application, I do it like this:

public void put(String fileName, String key, String value)
{
    SharedPreferences sharedPref = getContext().getSharedPreferences(fileName, Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = sharedPref.edit();
    editor.putString(key, value);
    editor.commit();
}

now I spawned another thread that will work independently (something like Sync), I refer to sharedPreference, like this,

mContext.getSharedPreferences(fileName, Context.MODE_PRIVATE);

but all values ​​in this particular preference are returned as null, I'm doing something wrong,

PS: - If I kill the application and the same thread appears again, I can get the values ​​(this is rather strange, but this happens, i.e. when the user logs in for the first time, this data is not available)

SharedPreferences, - ?

+4
4

, , , ,

MODE_MULTI_PROCESS MODE_PRIVATE ,

, !

0

, . - - XML, /data/data/pkg_name/preferences. , , , .

, , . , apply() commit().

, , .

+2

MODE_PRIVATE MODE_WORLD_READABLE.

+1
source

I had the same problem some time ago, trying to access my SharedPreferencefrom a service that was running in its own process. Here is how I solve it:

MultiProcessShared example :

public class GenericSharedPreferences {
public static final String TAG = GenericSharedPreferences.class.getSimpleName();

//region Logger
public static Logger sLogger;

static {
    sLogger = Logger.getLogger(TAG);
    if (BuildConfig.DEBUG)
        sLogger.setLevel(Level.ALL);
    else
        sLogger.setLevel(Level.OFF);
}
//endregion

public static MultiProcessShared.MultiProcessSharedPreferences getMultiProcessSharedReader(Context context) {
    return MultiProcessShared.getDefaultSharedPreferences(context);
}

public static MultiProcessShared.Editor getMultiProcessSharedWriter(Context context) {
    return MultiProcessShared.getDefaultSharedPreferences(context).edit();
}

}

My App SharedPreferences specific implementation:

public class MyAppSharedPreferences extends GenericSharedPreferences {
...
//region Country
    private static final String SHARED_APP_COUNTRY = "shared-app-country";

    public static Country getCountry() {
        String isoCode = getMultiProcessSharedReader(MyApp.getInstance()).getString(SHARED_APP_COUNTRY);
        CountryEnum countryEnum = CountryEnum.fromIso(isoCode);
        if (countryEnum == null)
            return null;
        return countryEnum.getCountry();
    }

    public static void setCountry(String isoCode) {
        if (TextUtils.isEmpty(isoCode))
            getMultiProcessSharedReader(MyApp.getInstance()).removeString(SHARED_APP_COUNTRY);
        else
            getMultiProcessSharedWriter(MyApp.getInstance()).putString(SHARED_APP_COUNTRY, isoCode);
    }
    //endregion
...
}

MyApp is the application that I linked in AndroidManifext.xml

Any question, feel free to ask!

+1
source

Source: https://habr.com/ru/post/1612915/


All Articles