Objective-C NSUserDefaults缓存阻止另一个应用程序准确读取更改 [英] Objective-C NSUserDefaults caching prevents another app from accurately reading changes

查看:74
本文介绍了Objective-C NSUserDefaults缓存阻止另一个应用程序准确读取更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以设置首选项的应用程序.它使用这些首选项,另一个应用程序也是如此.当我在第二个应用程序中使用以下代码时,它将一次正确读取首选项.

I have one application that sets preferences. It uses those prefs, and so does another application. When I use the following code in the second application, it reads the preferences correctly once.

    NSUserDefaults* settings = [NSUserDefaults standardUserDefaults];
    [settings addSuiteNamed:@"com.WVS.Wrestling-Tools"];
    [settings synchronize]; // this just a shot in the dark.. didn't work

    [self setScoreboardIndex:[settings integerForKey:@"matName"]];

在随后的相同代码调用中,我得到[settings integerForKey:@"matName"]始终是首次读取的值.

On subsequent calls of the same code, I get the [settings integerForKey:@"matName"] is always the value that was first read.

在第一个应用程序中,我正在使用共享用户默认值"控制器来读取/写入首选项.不知道这对这个问题是否重要.

In the first application, I'm using the Shared User Defaults Controller to read/write the preferences. Not sure if that matters to this issue or not.

如何强制第二个应用程序始终获取最新值?

How can I force the second application to always get the most recent values?

推荐答案

您应该能够在第二个程序中调用+[NSUserDefaults resetStandardUserDefaults]强制+[NSUserDefaults standardUserDefaults]创建一个新实例,并在下次调用它时从磁盘中重新加载值. .在 first 程序中调用-synchronize是确保将更新的默认值写入磁盘的正确方法(我不确定NSUserDefaultsController在这方面的行为).

You should be able to call +[NSUserDefaults resetStandardUserDefaults] in the second program to force +[NSUserDefaults standardUserDefaults] to create a new instance and reload values from disk the next time it's called. Calling -synchronize in the first program is the correct approach to make sure the updated defaults are written to disk (I'm not sure how NSUserDefaultsController behaves in this regard).

请注意,如果每次访问standardUserDefaults都调用+[NSUserDefaults resetStandardUserDefaults],这可能会导致性能下降,因为这大概意味着每次读取都会有磁盘命中.

Note that calling +[NSUserDefaults resetStandardUserDefaults] every time you access the standardUserDefaults may result in a performance penalty if you do so frequently, since it will presumably mean a disk hit on every read.

这篇关于Objective-C NSUserDefaults缓存阻止另一个应用程序准确读取更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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