NSUserDefaults 拒绝保存 [英] NSUserDefaults refuse to save

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

问题描述

启动一个新的单视图项目并更新主 ViewController 的 viewDidLoad.目的是检索和递增存储在 NSUserDefaults 中的值并保存它.

Start a new single view project and update the main ViewController's viewDidLoad. The intention is to retrieve and increment a value stored in NSUserDefaults and save it.

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *key = @"kTheKey";

    NSNumber *number = [[NSUserDefaults standardUserDefaults] objectForKey:key];
    NSLog(@"current value is %@", number);

    NSNumber *incremented = @(number.integerValue + 1);
    NSLog(@"new value will be %@", incremented);

    [[NSUserDefaults standardUserDefaults] setObject:incremented forKey:key];
    [[NSUserDefaults standardUserDefaults] synchronize];

    NSLog(@"reboot");
}

如果我从 Xcode 强制退出应用程序(或在实际使用中,重新启动设备),默认值通常不会保存.以下是一些示例输出:

If I force quit the app from Xcode (or in practical use, reboot the device), the defaults are frequently not saved. Here is some sample output:

current value is (null)
new value will be 1
reboot
current value is 1
new value will be 2
reboot
current value is 1
new value will be 2
reboot

似乎有一些时间组件 - 如果我在重新启动前等待 3 秒以上,则更有可能保存默认值.请注意,通过在停止执行之前等待几秒钟来允许"保存第一次执行.第二次执行在前一两秒停止,导致第三次运行中记录的值没有变化.这可以在运行 iOS 8.1 的 iPad Air 2 上重现.

There appears to be some time component - if I wait 3+ seconds before rebooting, it is more likely the defaults will save. Note that the first execution was 'allowed' to save by waiting for a few seconds before stopping execution. The second execution was stopped in the first second or two, leading to the unchanged values logged in the the third run. This is re-produceable on my iPad Air 2 running iOS 8.1.

这是什么原因造成的?

推荐答案

这是正常行为.

用户默认设置排队等待保存.当您强制退出"应用程序时,您并没有给它时间这样做.

User defaults get queued to save. When you "force quit" the app you're not giving it time to do that.

我假设在 Xcode 上你的意思是点击停止.你提到了exit(0);.对于 iOS 应用程序来说,这两种情况都不是正常的".此类强制退出不应在 iOS 应用中完成.

I assume that on Xcode you mean hitting stop. And you mention exit(0);. Neither of things are "normal" to an iOS app. This type of force quitting should not be done in an iOS app.

当用户以正常方式退出应用程序(多任务视图和向上滑动应用程序)时,它实际上并没有立即退出.它从视图中移除,就好像它确实存在一样.但是之后用户默认值将被写出.几秒钟后.当他们按下主页按钮时也是如此.

When the user quits an app the normal way (multi-task view and sliding the app up) it doesn't actually quit right then. It removes from view as if it does. But the user defaults will then get written out after that. Up to several seconds after. Same when they hit the home button.

文档完整地解释了应用生命周期.使用消息.并且您应该在收到这些消息时强制清除默认值.像这样输入你的初始化或 ViewDidLoad:

The documentation completely explains the app life cycle. Use the messages. And you should force flush out the defaults when these messages are received. Put in your initialization or ViewDidLoad like this:

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movingToBackground:) name:UIApplicationWillResignActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movingToForeground:) name:UIApplicationDidBecomeActiveNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDefaults:) name:UIApplicationWillTerminateNotification object:nil];

然后像这样创建movingToBackgroundmovingToForegroundupdateDefaults方法:

Then create the methods movingToBackground, movingToForeground, and updateDefaults like this:

-(void) updateDefaults: (NSNotification *) notification {
     [[NSUserDefaults standardUserDefaults] synchronize];
}

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

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