并非所有 NSUserDefaults 都在应用程序重新启动时恢复 [英] Not all NSUserDefaults are restored on app relaunch

查看:77
本文介绍了并非所有 NSUserDefaults 都在应用程序重新启动时恢复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将 2 个自定义列表转换​​为 json 字符串并将其存储在 NSUserDefaults 中.像这样:-

I am converting 2 custom lists into a json string and storing it the NSUserDefaults. Something like so:-

NSUserDefaults.StandardUserDefaults.SetString(JsonConvert.SerializeObject(stationList.Take(50)), "StationList1");
NSUserDefaults.StandardUserDefaults.SetString(JsonConvert.SerializeObject(stationList.Skip(50).Take(50)), "StationList2");  

如果我像下面那样在保存它们后立即尝试检索它们,我会得到保存的值:-

If I try and retrieve them immediately after saving them like below I get the saved values:-

savedStationList1 = NSUserDefaults.StandardUserDefaults.StringForKey("StationList1");
  savedStationList2 = NSUserDefaults.StandardUserDefaults.StringForKey("StationList2");  

但问题是如果我重新启动应用程序,并尝试在代码的另一部分获取上述值,我只能获取以下值:-

But the issue is if I restart the app, and try to get the above values in another part of the code, I only get the value for:-

savedStationList2 = NSUserDefaults.StandardUserDefaults.StringForKey("StationList2");  

并且下面的值始终为 null :-

and the value for below is always null :-

 savedStationList1 = NSUserDefaults.StandardUserDefaults.StringForKey("StationList1");

我不会在应用内的任何地方覆盖这些值.有什么办法可以解决这个问题吗?

I do not override these values anywhere within the app. Is there a way I can solve this?

感谢任何帮助

推荐答案

虽然 iOS 系统本身确实存储了通过userdefaults"添加的数据,但它可能不会立即执行此操作.我建议添加以下行:

Although natively the iOS system does store data added through 'userdefaults' it may not do this instantly. I would suggest adding the following line:

NSUserDefaults.StandardUserDefaults.Synchronise();

将数据存储到标准用户默认值后,运行同步并测试您是否可以提取数据,您应该会发现现在可以正常工作了.

After you store data to standard user defaults run the synchronise and test that you can extract the data, you should find that this will now work correctly for you.

这篇关于并非所有 NSUserDefaults 都在应用程序重新启动时恢复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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