A question about SharedPreferences

SharedPreferences can be used to save some very simple data, which corresponds to a key-value. However, a problem recently encountered is that when there are multiple processes, there is no way to save data synchronously. See that a solution is Context.MODE_MULTI_PROCESSto perform multi-threaded access. When introducing this method on the official website, it looks like this:

This constant was deprecated in API level 23.
MODE_MULTI_PROCESS does 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 as ContentProvider.

SharedPreference loading flag: when set, the file on disk will be checked for modification even if the shared preferences instance is already loaded in this process.
This behavior is sometimes desired in cases where the application has multiple processes, all writing to the same SharedPreferences file. 
Generally there are better forms of communication between processes, though.

This was the legacy (but undocumented) behavior in and before Gingerbread (Android 2.3) and this flag is implied when targetting such releases. 
For applications targetting SDK versions greater than Android 2.3, this flag must be explicitly set if desired.

This method has been deprecated above 6.0, and at least 2.3 is supported. And it's best to use ContentProvider.
If you must use it, try to avoid operating SharedPreferences at the same time. In fact, I also found some open source libraries, but I don't think it is necessary, so let's forget it.

Put some links:
- https://github.com/android-cn/android-discuss/issues/135
- http://zmywly8866.github.io/2015/09/09/sharedpreferences-in-multiprocess.html

Guess you like

Origin blog.csdn.net/Ser_Bad/article/details/51554823