Not all NSUserDefaults are restored on app relaunch

纵然是瞬间 提交于 2019-12-25 05:19:30

问题


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");  

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?

Any help is appreciated


回答1:


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.



来源:https://stackoverflow.com/questions/43631243/not-all-nsuserdefaults-are-restored-on-app-relaunch

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!