MODE_MULTI_PROCESS的共享preferences不工作 [英] MODE_MULTI_PROCESS for SharedPreferences isn't working

查看:2436
本文介绍了MODE_MULTI_PROCESS的共享preferences不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有自己的进程中运行独立于主应用程序过程中SyncAdapter。

I have a SyncAdapter running on its own process separately from the main app process.

我用在我的共享preferences一个静态的包装类创建的过程中负载静态对象(应用程序的onCreate),像这样:

I'm using a static wrapper class around my SharedPreferences that creates a static object on process load (Application's onCreate) like so:

myPrefs = context.getSharedPreferences(MY_FILE_NAME, Context.MODE_MULTI_PROCESS | Context.MODE_PRIVATE);

该包装具有get和set方法,就像这样:

The wrapper has get and set methods, like so:

public static String getSomeString() {
    return myPrefs.getString(SOME_KEY, null);
}

public static void setSomeString(String str) {
    myPrefs.edit().putString(SOME_KEY, str).commit();
}

无论SyncAdapter和应用程序使用该包装类编辑从preFS得到,这个工作有时但很多时候,我看到了SyncAdapter变老/缺少preFS上访问到preFS ,而主要的应用程序看到了最近的变化正常。

Both SyncAdapter and app uses this wrapper class to edit and get from the prefs, this works sometimes but a lot of times I see the SyncAdapter getting old/missing prefs on accesses to the prefs, while the main app sees the recent changes properly.

据我认为MODE_MULTI_PROCESS标志应该工作,我希望它,让这两个进程,看看最新更改的文档,但它不能正常工作。

According to the docs I think the MODE_MULTI_PROCESS flag should work as I expect it to, allowing both processes to see latest changes, but it doesn't work.

更新:

X90 的建议下,我试着不采取使用静态共享preferences对象,而是调用getShared preferences每个获得/套方法。 这引起了新的问题,其中的preFS文件被删除(!!!)在多进程并发访问。 也就是我在logcat中看到:

Per x90's suggestion, I've tried refraining from using a static SharedPreferences object and instead calling getSharedPreferences on each get/set method. This caused a new issue, where the prefs file gets deleted (!!!) on multi-process simultaneous access. i.e. I see in the logcat:

(process 1): getName => "Name"
(process 2): getName => null
(process 1): getName => null

和从该点保存的共享preferences对象上的所有的preFS被删除。

and from that point all the prefs saved on the SharedPreferences object were deleted.

这可能是一个警告我在日志中看到的结果:

This is probably a result of another warning I see in the log:

W/FileUtils(21552): Failed to chmod(/data/data/com.my_company/shared_prefs/prefs_filename.xml): libcore.io.ErrnoException: chmod failed: ENOENT (No such file or directory)

PS这不是一个确定性的问题,我看到上面的日志后崩溃发生,但不能重建尚未在同一设备上,并且到现在为止似乎如果不是发生在其他设备上。

P.S this is not a deterministic issue, I saw the above logs after a crash happened, but couldn't recreate yet on the same device, and until now it didn't seem to happen on other devices.

其他更新:

我已经提交了一份bug报告在此,写一个小的测试方法,以确认这确实是一个Android的问题,明星是在<一后href="https://$c$c.google.com/p/android/issues/detail?id=66625">https://$c$c.google.com/p/android/issues/detail?id=66625

I've filed a bug report on this, after writing a small testing method to confirm this is indeed an Android issue, star it at https://code.google.com/p/android/issues/detail?id=66625

推荐答案

有完全相同的同样的问题,我的解决办法是写一个的ContentProvider来替换老的共享preferences。它的工作原理100%多进程。

Had exactly the same problem and my solution was to write a ContentProvider based replacement for the SharedPreferences. It works 100% multiprocess.

我为我们所有的库。下面是结果: https://github.com/grandcentrix/tray

I made it a library for all of us. Here is the result: https://github.com/grandcentrix/tray

这篇关于MODE_MULTI_PROCESS的共享preferences不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆