3

my shared prefs are defined like this:

sharedPreferences = context.getSharedPreferences(Consts.SHARED_PREFS_NAME, Context.MODE_MULTI_PROCESS);

my receiver is defined in a different process:

<receiver android:name="com.my.name" android:enabled="true" android:process=":myProcessName" />

the processes used are different and the contexes are also different. the problem is, using the same boolean preference in both the apps process and my broadcastreceiver process returns unexpected values, as if the reading and writing are not on the same shared boolean preference.

Any ideas?

GrIsHu
  • 29,068
  • 10
  • 64
  • 102
Eitan Schwartz
  • 463
  • 6
  • 19

2 Answers2

3

I wrote a library to come around this problem: https://github.com/grandcentrix/tray

It uses an internal ContentProvider to store the data and is a full replacement for the SharedPreferences but with multiprocess support.

passsy
  • 5,162
  • 4
  • 39
  • 65
0

Ok, finally understood. The base of the problem was in saving the sharedPreferences in a static variable.

While sharedPrefs synchs multi process reading and writing when in MODE_MULTI_PROCESS, the static variable holding my sharedPrefs instance was not updating correctly.

It is a slightly costly action but calling a new instance of sharePrefs every time you're reading data is helpful.

Update: Unfortunately since the results from this issue are inconsistent the above "solution" is, in fact, not at all a solution.

Eitan Schwartz
  • 463
  • 6
  • 19