16

I have a sandboxed app for OSX and I saved some data in NSUserDefaults, everything was fine until I deleted the plist file from the ~Library/Preferences/ directory.

I thought the app should recreat it but it did not. When I debug I see that

[[NSUserDefaults standardUserDefaults] synchronize] method returns YES

and data are saved in the class but when I restart the app user defaults is empty.

Even if I copy those plist file from backup to the Preferences directory, the app does not see it.

Of course the plist should be saved in the Containers/my app bundle id/Data/Library/Preferences and when I copy the plist file from backup to that directory, the app can see it, but why that file is not recreated when I deleted it?

Does anybody know why is that?

Bartosz Bialecki
  • 4,391
  • 10
  • 42
  • 64
  • 4
    I don't understand the down votes. Perfectly legit question. I am also having issues with NSUserDefaults not persisting. I want to test my app launching from a 'blank' slate. So I trash my sandbox container, once I do that NSUserDefaults never persist until I restart the OS. Logging in/out doesn't do anything, only restarting helps. – Brad G May 23 '14 at 18:22
  • 1
    Thanks @Brad Goss. I had the same issue after cleaning out my sandbox, debugged for hours, though I was going crazy. Rebooting OSX seemed to have solved it. – staeryatz Nov 23 '15 at 04:13

2 Answers2

2

Check this question - might be related to caching or prefs location when running in the sandbox:

Mac sandbox created but no NSUserDefaults plist

Community
  • 1
  • 1
Jay
  • 6,572
  • 3
  • 37
  • 65
  • synchronize function should sync cache with database on the disk, so I think there is no problem with caching. Preferences should be placed in the Containers/app/Data/Library/Preferences but this directory is also empty. – Bartosz Bialecki Mar 10 '14 at 08:51
  • This bit me too. Wiping the Container (while in development) caused all kinds of grief. "defaults delete", etc. couldn't help, and `synchronize` wrote nothing. – Hal Mueller Jan 22 '16 at 03:01
1

I had this problem, which is caused by deleting the files manually. What you can do instead is to use the defaults terminal command. This way I was able to reset the defaults without breaking things. Replace "bundle-identifier" with your bundle id.

To view the current values:

defaults read bundle-identifier

To remove stored values:

defaults delete bundle-identifier
Henrik
  • 3,908
  • 27
  • 48