NSUserDefaults同步不保存 [英] NSUserDefaults synchronize not saving on

查看:76
本文介绍了NSUserDefaults同步不保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个测试人员提交的错误,如果他执行某个动作,然后重新启动他的手机(通过按下主页和睡眠/唤醒按钮几秒钟)该应用程序不会持久状态。

I have a bug submitted by a tester that if he performs an action and then reboots his phone (by pressing the home and Sleep/Wake button down for a few seconds) the app is not persisting state.

我能够重现这个问题。 [NSUserDefaults synchronize] 调用但是当我在重新启动后重新启动应用程序时,NSUserDefaults中的值未保存。

I have been able to reproduce this issue. [NSUserDefaults synchronize] is getting called but when I restart the app after the reboot, the value inside NSUserDefaults was not saved.

有没有人知道如果将存储同步到稍后保存到磁盘的缓冲区?如果是这样,我如何刷新缓冲区(我认为同步与刷新相同,但显然不是。)

Does anybody know if synchronize stores to a buffer which is later saved to disk? If so, how do I flush the buffer (I thought synchronize was the same as a flush, but apparently not.)

(编辑)
换句话说:

(edit) In other words:

assert([[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == NO);
[[NSUserDefaults standardUserDefaults] setBool: YES forKey: MY_KEY];
[[NSUserDefaults standardUserDefaults] synchronize];

将应用程序保留在前台并在调用上述设备后重新启动设备,然后启动设备并重新运行该应用程序。断言应该第二次触发,但有时它不会触发。

leave the app in the foreground and reboot the device after the above is called, then start the device back up and re-run the app. The assert should fire the second time around, but sometimes it doesn't.

非常具体......我创建了一个单独的视图应用程序并将以下代码放入viewDidLoad

To be very specific... I created a single view application and put the following code in the viewDidLoad

#define MY_KEY @"MY_KEY"

- (void)viewDidLoad
{
    [super viewDidLoad];
    BOOL key = [[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY];
    NSLog(@"[[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == %@", key ? @"YES" : @"NO");
    [[NSUserDefaults standardUserDefaults] setBool: !key forKey: MY_KEY];
    [[NSUserDefaults standardUserDefaults] synchronize];
    NSLog(@"reboot now.");
}

以下是该应用程序的三次运行输出:

Here is the output of three runs of the app:

2013-05-31 12:17:44.127 syncTest[162:907] [[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == YES
2013-05-31 12:17:44.133 syncTest[162:907] reboot now.
2013-05-31 12:18:49.771 syncTest[128:907] [[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == NO
2013-05-31 12:18:49.778 syncTest[128:907] reboot now.
2013-05-31 12:19:41.388 syncTest[124:907] [[NSUserDefaults standardUserDefaults] boolForKey: MY_KEY] == NO
2013-05-31 12:19:41.397 syncTest[124:907] reboot now.

请注意,输出为YES,NO,NO但它应该是YES,NO ,YES

Note that the output was "YES, NO, NO" but it should have been "YES, NO, YES"

推荐答案

我遇到了同样的问题,我发现我有两个[NSUserDefaults standardUserDefaults]对象的引用不同的类,因为nsdefault是一个单例类soo如果你可能已经引用 [NSUserDefaults standardUserDefaults] 尝试从第一个引用完成它,那么之前调用它的那个将会有它让实例从那里释放,然后在你的另一个类中调用实例并希望同步完成

i had gone through the same problem and i identify that i had two reference of [NSUserDefaults standardUserDefaults] object on different classes because nsdefault is a singleton class soo the one called it earlier will have it if u might had already reference of [NSUserDefaults standardUserDefaults] try to finish it from the first reference and let the instance to be free from there and then call the instance at your other class and synchronize hopefully will be done

这篇关于NSUserDefaults同步不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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