IOS 10 中的 UserDefaults 有时会显示旧值 [英] UserDefaults in IOS 10 is sometimes showing old value

查看:72
本文介绍了IOS 10 中的 UserDefaults 有时会显示旧值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在我的应用程序上测试一些东西,我将当前使用的主题保存为一个字符串:Light"与Dark".

I am testing something on my app where I save the current used theme as a string: "Light" vs "Dark".

我刚刚意识到,在某些时候,当我不断从亮到暗切换(读取和写入字符串到 UserDefaults)时,我有时会得到旧值:

I just realized that at some point when I keep switching from light to dark (reading and writing the string to UserDefaults) I sometimes get on old value:

Light -> Dark -> Light -> Dark -> Light -> Light

我已将日志添加到读取和写入值的位置并正确执行.我是这样测试的:

I have added logs to where I read and write the value and its doing it correctly. This is how I test:

 1. Start app 
 2. Change theme (save string to UserDefaults)
 3. Stop app
 4. Start app (read string from UserDefaults)

我看到其他一些帖子说IOS 10 中的UserDefaults 有问题,解决方案是重新启动设备或mac.

I saw some other posts which says there is a problem with UserDefaults in IOS 10 and the solution is to restart device or mac.

我明白了,但这不意味着问题也会出现在用户设备上吗?有没有人遇到过这种问题?有什么解决办法吗?

I get this, but doesn't this mean the issue will appear on the user device as well ? Has anyone encounter this kind of issue ? Any solution ?

我使用 Xcode 8.2.1 和 IOS 10,Swift 3.我在我的真实设备上进行测试,而不是模拟器.

I am using Xcode 8.2.1 with IOS 10, Swift 3. And I am testing on my real device, not simulator.

推荐答案

您确定在保存数据后调用 [[NSUserDefaults standardUserDefaults] synchronize]; 吗?

Are you sure that you call [[NSUserDefaults standardUserDefaults] synchronize]; after saving Data?

这篇关于IOS 10 中的 UserDefaults 有时会显示旧值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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