简体   繁体   中英

SharedPreferences in Android not getting recently updated values across multiple running processes

Description: I have two apps App1 and App2. I am using App1 to store some key value pair in SharedPreferences. I am accessing the same key value in App2.

I launch app1. Create a key with value abc. Now I keep app1 in background and launch App2 and I change the key value to def.

When I launch app1 from background to foreground and access the key value. Value retrieved is abc instead of updated value def. If I kill App2 from background and relaunch it then only updated value is getting reflected

SharedPreferences has never supported multiple processes, let alone multiple apps. The documentation explicitly states:

Note: This class does not support use across multiple processes.

该值不会更新,因为 SharedPreferences 无法识别跨多个进程的数据更改。

The shared preferences of an app are stored in a separate file within the app's internal storage which no other apps can access. The shared preferences of two different apps are located in two different locations and are independent of each other.

So the shared preferences of an app cannot be accessed by any other apps and change in the shared preferences of an app is not reflected in any other apps.

When process reads the data from the SharedPreferences, it copies the values (data) and puts those data inside it's process inside the cache. It does this because reading/writing from disk is too slow. Generally, System creates cache process for Android Components. Now, your data from the SharedPreferences is stored in the component's cache process. Cache process has it's own memory space. Until the component is using the memory, it locks (critical section) in order to prevent other can't access that memory. The cache process should be killed so that other processes (or components) can now be able to access it. This is why when you relaunch you see the update.

Critical Section: https://en.wikipedia.org/wiki/Critical_section

Different Processes: https://developer.android.com/guide/components/activities/process-lifecycle.html

In thread-level, you use the keyword volatile , so that you see the one thread updated data from second thread. But SharedPreferences can't be volatile because it is persistent storage.

That's why they explicitly stated SharedPreferences doesn't support on multiple processes

Sharedpreferences are not process safe as per their documentation. I ran into a similar problem. I was using a value from sharedpreferences within an activity and then in a service with another process. The same value was behaving like two different values.

Well i used this lib to solve this problem. Its basically the same approach and syntax and its process safe.

SharedPreferences has never supported multiple processes, let alone multiple apps. The documentation explicitly states:

Note: This class does not support use across multiple processes.

SharedPreferences had to be replaced with content provider in my case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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