ContextIml对象中通过Map集合缓存了多个SharedPreference对象,该Map集合是全局的,key对应shared_prefs文件名,value对应SharedPreferenceImpl(SharedPreferences接口的实现类)对象。因此,shared_prefs文件与内存中的SharedPreferencesImpl对象是一一对应的关系。
shared_prefs xml文件中的键值对数据则存储在SharedPreferencesImpl对象内部的Map集合中.因此shared_prefs xml文件中的键值对数据和SharedPreferencesImpl中的Map对象是一一对应的关系。
Context.getSharedPreferences(String name,int mode)方法解读
优先从全局缓存Map中读取,有则返回,没有则新建一个SharedPreferencesImpl对象存储到缓存Map中,由此可见一个shared_prefs文件在内存中只对应一个SharedPreferencesImpl对象,源码如下:
//step 1 获取SharedPreferences对象
public SharedPreferences getSharedPreferences(String name, int mode) {
SharedPreferencesImpl sp;
synchronized (ContextImpl.class) {
if (sSharedPrefs == null) {
sSharedPrefs = new ArrayMap<String, ArrayMap<String, SharedPreferencesImpl>>();
}
final String packageName = getPackageName();
ArrayMap<String, SharedPreferencesImpl> packagePrefs = sSharedPrefs.get(packageName);
if (packagePrefs == null) {
packagePrefs = new ArrayMap<String, SharedPreferencesImpl>();
sSharedPrefs.put(packageName, packagePrefs);
}
// At least one application in the world actually passes in a null
// name. This happened to work because when we generated the file name
// we would stringify it to "null.xml". Nice.
if (mPackageInfo.getApplicationInfo().targetSdkVersion <
Build.VERSION_CODES.KITKAT) {
if (name == null) {
name = "null";
}
}
sp = packagePrefs.get(name);
if (sp == null) {
File prefsFile = getSharedPrefsFile(name);
sp = new SharedPreferencesImpl(prefsFile, mode);
packagePrefs.put(name, sp);
return sp;
}
}
if ((mode & Context.MODE_MULTI_PROCESS) != 0 ||
getApplicationInfo().targetSdkVersion < android.os.Build.VERSION_CODES.HONEYCOMB) {
// If somebody else (some other process) changed the prefs
// file behind our back, we reload it. This has been the
// historical (if undocumented) behavior.
sp.startReloadIfChangedUnexpectedly();
}
return sp;
}
第一次获取SharedPreferences对象时会首先创建一个shared_prefs文件,然后读取文件的数据存储到SharedPreferences对象的Map中,注意此操作是在子线程中进行的,此操作完成后会将mLoaded标记置为true,代表shared_prefs文件已经加载完成.
//step 2 Map中没有则新建一个SharedPreferencesImpl对象
SharedPreferencesImpl(File file, int mode) {
mFile = file;
mBackupFile = makeBackupFile(file);
mMode = mode;
mLoaded = false;
mMap = null;
startLoadFromDisk();
}
//step 3 第一次创建SharedPreferencesImpl后,在子线程中执行读取xml文件
private void startLoadFromDisk() {
synchronized (this) {
mLoaded = false;
}
new Thread("SharedPreferencesImpl-load") {
public void run() {
synchronized (SharedPreferencesImpl.this) {
loadFromDiskLocked();
}
}
}.start();
}
//step 4 将xml文件中的数据读取到SharedPreferencesImpl对象内部的Map中,并将mLoaded赋值为true,然后唤醒其他线程继续执行
private void loadFromDiskLocked() {
if (mLoaded) {
return;
}
if (mBackupFile.exists()) {
mFile.delete();
mBackupFile.renameTo(mFile);
}
// Debugging
if (mFile.exists() && !mFile.canRead()) {
Log.w(TAG, "Attempt to read preferences file " + mFile + " without permission");
}
Map map = null;
StructStat stat = null;
try {
stat = Os.stat(mFile.getPath());
if (mFile.canRead()) {
BufferedInputStream str = null;
try {
str = new BufferedInputStream(
new FileInputStream(mFile), 16*1024);
map = XmlUtils.readMapXml(str);
} catch (XmlPullParserException e) {
Log.w(TAG, "getSharedPreferences", e);
} catch (FileNotFoundException e) {
Log.w(TAG, "getSharedPreferences", e);
} catch (IOException e) {
Log.w(TAG, "getSharedPreferences", e);
} finally {
IoUtils.closeQuietly(str);
}
}
} catch (ErrnoException e) {
}
mLoaded = true;
if (map != null) {
mMap = map;
mStatTimestamp = stat.st_mtime;
mStatSize = stat.st_size;
} else {
mMap = new HashMap<String, Object>();
}
notifyAll();
}
对shared_prefs文件的读写操作都会判断mLoaded状态,如果mLoaded为false,则当前调用线程会等待。
//写操作会等待mLoaded=true
public Editor edit() {
// TODO: remove the need to call awaitLoadedLocked() when
// requesting an editor. will require some work on the
// Editor, but then we should be able to do:
//
// context.getSharedPreferences(..).edit().putString(..).apply()
//
// ... all without blocking.
synchronized (this) {
awaitLoadedLocked();
}
return new EditorImpl();
}
//读操作也会等待mLoaded=true
public String getString(String key, String defValue) {
synchronized (this) {
awaitLoadedLocked();
String v = (String)mMap.get(key);
return v != null ? v : defValue;
}
}
每次edit都会新建一个Editor对象,每次写操作之后,数据都是保存中Editor对象内部的临时容器Map中,且clear操作也只是改变其内部的clear字段值为true,
public final class EditorImpl implements Editor {
private final Map<String, Object> mModified = Maps.newHashMap();
private boolean mClear = false;
public Editor putString(String key, String value) {
synchronized (this) {
mModified.put(key, value);
return this;
}
}
...
public Editor clear() {
synchronized (this) {
mClear = true;
return this;
}
}
当进行了commit或者apply操作之后,会将临时容器Map和SharedPreferencesImpl内部的Map进行数据对比,然后将最终的Map数据写入到shared_prefs文件中.
commit会直接在当前线程提交
apply则会放入一个线程池中执行。注意ActivityThread handleStopActivity方法中会检查这个线程池中的任务,如果任务未完成则会等待。
private void handleStopActivity(IBinder token, boolean show, int configChanges, int seq) {
// 省略无关。。
// Make sure any pending writes are now committed.
if (!r.isPreHoneycomb()) {
QueuedWork.waitToFinish();
}
// 省略无关。。
}
public static void waitToFinish() {
Runnable toFinish;
while ((toFinish = sPendingWorkFinishers.poll()) != null) {
toFinish.run();
}
}
此外SharedPreferences并不支持多进程,对于mode是多进程模式也仅仅是重新加载文件到内存中。
另外SharedPreferences 内部是通过加锁保证线程安全的。