Gargoyle
Gargoyle

Reputation: 10324

Where are NSUserDefaults stored on El Capitan?

In my Mac XCode project I have a bundle identifier of "com.gargoylesoft.DTS-macOS". I'm trying to blow away the stored defaults on this user so I did this:

find ~ -name 'com.gargoylesoft.D*' -exec rm -rf {} \;

When I then run the app, through Xcode, I'm still getting values from previous runs when I query NSUserDefauls. Where in the world is it storing this???

Upvotes: 1

Views: 282

Answers (1)

Mecki
Mecki

Reputation: 132979

You must never delete (or edit) preference files directly on macOS! macOS caches preferences using a background daemon named cfprefsd. The daemon won't notice that you delete (or alter) the files, it keeps a cached copy in RAM that it will only expire if your system gets memory pressure. Memory pressure means: an app needs RAM but no unused RAM is available, then the system signals memory pressure, causing all kind of system services to flush their caches as that is still better than swapping to disk (will also work in your own application, just try to always use NSCache when caching data, as it will automatically react to memory pressure).

If you want to delete prefs, use defaults in terminal, like this

# defaults delete com.gargoylesoft.DTS-macOS

This will delete the file and it will make sure that also cfprefsd flushes its caches for that file.

Another advantage of defaults is that you also don't need to know if an application is sandboxed or not, as a normal application stores its prefs in

~/Library/Preferences

whereas a sandoxed one stores them in

~/Library/Containers/BUNDLE_ID/Data/Library/Preferences/

but defaultswill always look in the appropriate place.

Once you deleted a pref file by hand, you need to kill cfprefsd of your user (careful, there's one for your user and one for root - you don't want to kill the root one if these were user settings) and the system should then restart it for you. Or just reboot or at least log out and back in, both will restart it as well.

Upvotes: 1

Related Questions