4

I am seeing this strange behavior, which seems to be specific to my concrete device or my building environment (my colleague does not have this problem when building the same code). Every time I start the app from Xcode data in NSUserDefaults is gone. Same happens when I kill the app and restart it.

I have tried restarting Xcode, my laptop, device, cleaning the project and derived data - nothing helped. I do call synchronize() after updating data, so it should be persisted. But it does not. And I don't see any errors in console.

What is the cause of this problem? What do I do to fix it? Is this caused by some fancy hidden project setting?

I would also like to know whether data is reset on restart or not saved when synchronize() is called. Can I inspect the contents of file in which NSUserDefaults are saved?


UPDATE: An interesting detail. After killing the app in Simulator, I cannot start it from Xcode again (getting "The operation couldn’t be completed. (LaunchServicesError error 0.)"). I have to use iOS Simulator -> Reset Content and Settings to be able to run the app in emulator again.


UPDATE 2: I tried to find the file in which iPhone Simulator stores NSSharedPreferences as suggested here, but it is not there.

Community
  • 1
  • 1
Andrii Chernenko
  • 9,873
  • 7
  • 71
  • 89
  • Try not killing the app but close it like normal app (two click on come button, command+shift+h in simulator) – Eduardo Iglesias Apr 23 '15 at 16:28
  • @EduardoIglesias Yes, this way settings will not be reset. But should I also ask my users not to kill the app? – Andrii Chernenko Apr 23 '15 at 16:30
  • You synchronise the user defaults when there is something changed that you want to survive. [[NSUserDefaults standardUserDefaults] synchronize]. – gnasher729 Apr 23 '15 at 16:34
  • 1
    @gnasher729 please read the question more carefully, I DO call synchronize() – Andrii Chernenko Apr 23 '15 at 16:37
  • @deville what do you mean kill the app? By closing the app or killing it with xCode? By force killing it by xCode its like a crash and sometimes if defaults doesn't write the data in the SSD you will lost that. Edit: Do you use CocoaPodS? – Eduardo Iglesias Apr 23 '15 at 16:42
  • @EduardoIglesias by killing I mean closing it using fast application switcher (double-tap home and swipe the app up). And yes, I do use CocoaPods. – Andrii Chernenko Apr 23 '15 at 16:46
  • @deville Maybe you can try this. http://stackoverflow.com/questions/25889189/unable-to-run-app-in-simulator-an-error-was-encountered-while-running-domain – Eduardo Iglesias Apr 23 '15 at 16:50
  • @EduardoIglesias I don't use `ShareKit`, my app is not for AppleWatch. – Andrii Chernenko Apr 23 '15 at 16:56

2 Answers2

3

There is no fancy setting that would disable persisting into NSUserDefaults, however, there are few considerations that you need to take into account:

  1. Be sure you archive and unarchive objects other than simple value ones before adding and retrieving them from NSUserDefaults
  2. Be sure you understand the scope of persisted data (App, Device,...), look at the appropriate methods inside NSUserDefault class.
  3. Make sure your 3rd party libs don't reset NSUserDefault, it was the problem in my case. Debug at the launch of the the app and try to figure out if you have the value at the start and when you "loose it". This helped in my case.
mbpro
  • 2,460
  • 3
  • 22
  • 37
0

Try to make a new "Single View Application" and add the code which you are using to save the data in NSUserDefault, in didFinishLaunchingWithOptions. See if the problem persists.

swapnilagarwal
  • 1,126
  • 8
  • 16