Is this a bug, or just me goofing things up again?....
SUMMARY
I call CFPreferencesSynchronize() followed by CFPreferencesCopyValue()
with the domain argument in both set to kCFPreferencesAnyUser. I
expect that this should always give me the current value of the given
key on the disk, in file /Library/Preferences/com.whatever.bundle.
However, if I remove the last key in the domain by calling
CFPreferencesSetValue(), which deletes the plist file,
CFPreferencesCopyValue() continues to return the last value it found
instead of NULL.
If the program in which it is running is quit and re-launched, then
CFPreferencesCopyValue() returns NULL as expected.
STEPS TO REPRODUCE
• In Mac OS 10.5.2, compile a Foundation Tool with this code:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
while (true) {
NSString *myValue = nil ;
CFStringRef bundleIdentifier = CFSTR("com.bug.CFPrefs") ;
BOOL ok = CFPreferencesSynchronize(bundleIdentifier,
kCFPreferencesAnyUser,
kCFPreferencesCurrentHost) ;
NSLog(@"Sync ok=%d for bundleID %@", ok, bundleIdentifier) ;
• In your favorite plist editor such as PlistEdit Pro, create a file
at /Library/Preferences/com.bug.CFPrefs.
• In the plist editor, enter the string key-value pair myKey = "AAA"
• Save the file.
Expected and Actual Result: Console begins spitting out:
value of myKey = AAA
• In the plist editor, change value of myKey to "BBB".
• Save the file again.
Expected and Actual Result: Console begins spitting out:
value of myKey = BBB
• In the plist editor, remove the key-value pair for myKey.
• Save the file again.
Expected and Actual Result: Console begins spitting out:
value of myKey = <null>
• In the plist editor, Undo, changing value of myKey back to "BBB".
• Save the file again.
Expected and Actual Result: Console begins spitting out:
value of myKey = BBB
• In the Finder, delete the file /Library/Preferences/com.bug.CFPrefs.
• Save the file again.
Expected Result: Console begins spitting out:
value of myKey = <null>
Actual Result: Console continues spitting out:
value of myKey = BBB
• Quit and re-launch the tool.
• Expected and Actual Result: Console begins spitting out:
value of myKey = <null>
DISCUSSION
I was hoping I could work around this by examining the array of apps
that currently have "anyUser" preferences:
Unfortunately, the above doesn't work as I expect either. It always
CFShows an empty array (count=0), despite the fact that I've got a
couple dozen plist files in /Library/Preferences.
So the only workaround I can think of is to break all the rules and
look for the existence of the plist file. Hold your nose!.....