Mavericks & Preferences

Another significant change in Mavericks, that caused a lot of frustration here is a change to the OS X Preferences system. This issue will only affect your application if you use NSUserDefaults/CFPreferences (which is the Apple recommend way of managing Preferences) and delete the preferences file or application container.

The Problem: Apple have always advised against directly manipulating the .plist preferences file in the finder or via other apps. This time external modification can lead to issues with your application.

Basically to test my default preferences, I deleted a prefs file of an application I was working on, only to find that the preferences didn’t actually reset, any changes to the preferences within the application didn’t stick either. It took me a long while to track down as I first assumed that there was something wrong with my code. In the end it turned out that a change to the OS X preferences system, means the safest way to reset or delete preferences is to use the delete function within NSUserDefaults from within your application (I suspect, they’re lining it up to do away with plist files and the preferences folder as a whole).

Once the prefs have been rodgered by deleting the file, it requires a restart or re-login to clear the NSUserDefaults cache and only then, will NSUserDefaults function correctly.

I would expect you to be able to just use the defaults command line tool to delete the app’s domain.

So, if obviously I have to expect all kind of headaches with any other new OSX release, why would I rely on the OSX preferences system at all? What would be the advantage of it?

I wonder wether I should not just continue to use a sqlite database within SpecialFolder.ApplicationData to store user preferences? The advantage is that I can use it x-platform.

I would if I were you!

OS X does store things like Open/Save dialog last folder and various OS level picker settings in the default prefs. The thing I liked the most was being able to store all kinds of things in the prefs file and then to use Xcode to edit my prefs file. I also loved the function, where by I can tell the OS to maintain settings for a window, so it automatically stores the window size and location for me, and then when the window re-opens it restores it.

Yeah, that would be handy - if it was x-platform. So now, instead of implementing this the OSX way, I did my own save/restore ‘meccano’ and again, the advantage is that it works on OSX / Windows / Linux.

If I’m not wrong this should work.
Mavericks cache preferences for speed and just periodically update the cache.
Using the recommended APIs (NSUserDefaults/CFPreferences) should force the cache update.
The defaults command use official APIs.

Have to try my app on Mavericks later at home. It uses CFPreferences…

It’s only an issue if you delete the prefs file or the application container, which of course I did…

The down side to doing this is that the system wide preference to restore open windows etc don’t work with your application so your application “feels” somehow not quite platform correct.

I did post a quick sample of a cross platform prefs class on the old blog that used NSUserDefaults on OS X, the registry on Windows & something custom on Linux (since it has no system wide standard)

Macoslib contains my “SmartPreferences” that does what Norman describes: It provides an easy-to-use API (similar to a dictionary) and works on all 3 platforms, even using the same file format (plist), and uses the correct APIs on OS X.

I use this in all my projects now, scrapping my old routines - SmartPreferences works brilliantly and is very easy to use, I highly recommend it.

Couldn’t agree more :slight_smile:

Thanks for the kudos! Nice to see it’s appreciated.