I am writing a date to SharedPreferences when ever my app retrieves the latest data from a feed. I now need to read that value however I cannot find much information on concurrency when reading/writing to SharedPreferences.
Would I have to use the synchronized keyword in my methods to prevent/lower the chances of causing a force close? Or have the folks at Google made SharedPreferences in a way that I shouldn’t worry about this?
For the record I do not think there is much likelihood of a collision due to the infrequent read and write operations although I would not like to assume that everything will just “work”.
Use the flag MODE_MULTI_PROCESS when opening the SharedPreferences. This was the legacy (but undocumented) behavior in and before Gingerbread (Android 2.3) and this flag is implied when targeting such releases. For applications targeting SDK versions greater than Android 2.3, this flag must be explicitly set if desired.
This constant was deprecated in API level 23.
MODE_MULTI_PROCESSdoes not work reliably in some versions of Android, and furthermore does not provide any mechanism for reconciling concurrent modifications across processes. Applications should not attempt to use it. Instead, they should use an explicit cross-process data management approach such asContentProvider.