Bug?:? CFPreferencesSynchronize() Remembers Value from Deleted File
Bug?:? CFPreferencesSynchronize() Remembers Value from Deleted File
- Subject: Bug?:? CFPreferencesSynchronize() Remembers Value from Deleted File
- From: Jerry Krinock <email@hidden>
- Date: Tue, 29 Apr 2008 17:21:01 -0700
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) ;
myValue = (NSString*)CFPreferencesCopyValue (CFSTR("myKey"),
bundleIdentifier,
kCFPreferencesAnyUser,
kCFPreferencesCurrentHost) ;
NSLog(@"value of myKey = %@", myValue) ;
if (myValue != NULL) {
CFRelease(myValue) ;
}
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:
1.0]] ;
}
[pool drain];
return 0;
}
• Run the tool.
• 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:
CFArrayRef appList = CFPreferencesCopyApplicationList(
kCFPreferencesAnyUser,
kCFPreferencesCurrentHost) ;
CFShow(appList) ;
CFRelease(appList) ;
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!.....
NSFileManager* fm = [NSFileManager defaultManager] ;
NSString* plath = [NSString stringWithFormat:@"/Library/Preferences/
%@.plist", bundleIdentifier] ;
BOOL fileExists = [fm fileExistsAtPath:plath] ;
If I then only call CFPreferencesCopyValue() if fileExists, I get the
result I need. Is there any better workaround?
Thanks,
Jerry
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden