iOS NSUserDefaults plist文件正在丢失更改 [英] iOS NSUserDefaults plist files are losing changes

查看:641
本文介绍了iOS NSUserDefaults plist文件正在丢失更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正面临一个不寻常的情况,我的应用程序的plist会自动设置为默认值,主要是在重启ipad / iphone之后。关于为什么会发生这种情况的任何想法?

We are facing an unusual scenario where my app's plist get set to default values automatically ,mostly after restarting the ipad/iphone. Any ideas on why this happening?

我们正在读这样的plist

We are reading the plist like this

[[NSUserDefaults standardUserDefaults] registerDefaults:[AppSetting globalConfig]];

+ (NSDictionary *) globalConfig {
    NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"settings" ofType:@"plist"];
    return [[[NSDictionary alloc] initWithContentsOfFile:plistPath] autorelease];
}

保存后我们将其写下来

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];   
[defaults setBool:self.isLogIn forKey:@"isLogin"];
[[NSUserDefaults standardUserDefaults] synchronize];

更多编辑...
这与这个自动恢复有什么关系。我在ipads中看到这条线已经恢复了..

some more edits... does this have any thing to do with this automatic restore. i am seeing this line in the ipads who's plist was restored..

<Error>: HID: The 'Passive' connection 'appName' access to protected services is denied.
<Error>: HID: The 'Rate Controlled' connection 'appName' access to protected services is denied.


推荐答案

您的应用程序正在写入NSUserDefaults而不会调用同步。应用程序进入后台,并因某些原因崩溃(或者直接崩溃) - 所以更改意味着保存到NSUserDefaults永远不会,并且看起来你的plist文件在它从未保存时会恢复。它可能会以不允许保存NSUserDefaults更改的方式关闭设备以杀死应用程序。

Your app is writing to NSUserDefaults without ever calling synchronize . The app goes to the background, and crashes for some reason (or just crashes outright) - so the changes meant to be saved to NSUserDefaults never are, and it appears your plist file is reverting when it was just never saved. It could be powering off the devices kill the apps in a way that is not allowing the save of NSUserDefaults changes.

或者您是否正在写入NSUserDefaults plist文件而不通过NSUserDefaults的?不要这样做。

OR are you writing to the NSUserDefaults plist files without going through NSUserDefaults? Don't do that.

或者测试人员正在使用相同iCloud帐户的多台设备上测试,这些设备正在为您的应用程序设备之间同步UserDefaults plist文件。

OR the testers are testing on multiple devices with the same iCloud account on both that are synchronizing the UserDefaults plist files between devices for your app.

编辑:为什么要从只读应用程序包中读取设置并使用 NSUserDefaults 来保存更改?他们不会做同样的事情。你永远不会读小麦 NSUserDefaults 写信给。

Why are you reading settings from the read-only app bundle and using NSUserDefaults to save changes? They will not do the same thing. You will never be reading wheat NSUserDefaults is writing to.

这篇关于iOS NSUserDefaults plist文件正在丢失更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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