• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: A Mac App & helper NSStatusItem - how to share preferences
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A Mac App & helper NSStatusItem - how to share preferences


  • Subject: Re: A Mac App & helper NSStatusItem - how to share preferences
  • From: Steve Cronin <email@hidden>
  • Date: Tue, 30 Dec 2008 15:14:58 -0600

Michael - So does the revised code below fix the leak?

After a bit more studying of the documentation, I offer these revised 'more Cocoa-friendly' updates:

The driving issue is a helper app which wants to access prefs written by the 'motherApp'.

- (NSDictionary *) prefDictionary {
CFStringRef appBundleID = (CFStringRef)motherAppBundleID;
//Core Foundation 'create rule' sez I own this -> I must dispose of it (because 'copy' or 'create' in CF method name)
CFDictionaryRef prefs = CFPreferencesCopyMultiple(NULL, appBundleID, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
//cast it into an auto-released Cocoa object
NSDictionary *result = [NSDictionary dictionaryWithDictionary: (NSDictionary *)prefs];
//take care of the low-level memory issue
CFRelease(prefs);
return result;
}
It also may be worth pointing out that this dictionary only gets the values from the permanent store.
So any registered defaults which the user has never changed do NOT show up in this dictionary.


Now instead of using the low-level API to obtain individual values simply access key-value pairs using the NSDictionary returned above.
This avoids having to write any further code to read motherApp's preferences.
There are some stern words in the docs that weigh against indiscriminate accessing using the low-level stuff, so just get the dictionary and be done with it!


Now if helper MUST update a motherApp preference:
- (void) setPrefValue:(id)preferenceValue forKey:(NSString *)preferenceKey {
CFStringRef appBundleID = (CFStringRef)motherAppBundleID;
CFPreferencesSetValue((CFStringRef)preferenceKey, (CFPropertyListRef)preferenceValue, appBundleID, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);


// as Jean-Daniel noted the line below is expensive and to be used only as required
//that being said - it ain't on the disk until its on the disk....
CFPreferencesSynchronize(appBundleID, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost);
}


NOTE: There is a CFPreferencesSetMultiple which allows for setting a dictionary's worth of keys at once as well as specifying an array of keys to be removed.
You still have to use CSPreferencesSynchronize if you want to get it written


I hope this is useful.
Steve

_______________________________________________

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


  • Follow-Ups:
    • Re: A Mac App & helper NSStatusItem - how to share preferences
      • From: Steve Cronin <email@hidden>
    • Re: A Mac App & helper NSStatusItem - how to share preferences
      • From: "Michael Ash" <email@hidden>
References: 
 >A Mac App & helper NSStatusItem - how to share preferences (From: Steve Cronin <email@hidden>)
 >Re: A Mac App & helper NSStatusItem - how to share preferences (From: "Kyle Sluder" <email@hidden>)
 >Re: A Mac App & helper NSStatusItem - how to share preferences (From: Steve Cronin <email@hidden>)

  • Prev by Date: NSTableView headers
  • Next by Date: Re: NSTableView headers
  • Previous by thread: Re: A Mac App & helper NSStatusItem - how to share preferences
  • Next by thread: Re: A Mac App & helper NSStatusItem - how to share preferences
  • Index(es):
    • Date
    • Thread