On Feb 23, 2011, at 3:41 AM,  Roberto Mauro <[email protected]> wrote:

> Ok,
> thank you to eskimo1 on the Apple Developer Forum I get how to setup a 
> notification for changes on prefs.
<snip>
> but every time prefDidChange is called when I try to get the CurrentSet from  
> kSCPrefCurrentSet as:
> 
> CFPropertyListRef current = SCPreferencesGetValue(prefs, kSCPrefCurrentSet);
> 
> it still returns me the previous one, like nothing is really changed, even if 
> in the Console I can read:
> CurrentSet updated to BE5263F0-0FED-4B6E-9786-E58E51F5CD1E (Automatic)


SCPreferences objects are cached and not re-read once created. If you use 
SCPreferences, you must release the SCPreferences object when you get a 
notification that it has changed, and re-create it. Hence the comment in 
prefsDidChange, "reread your view of the world". It makes things more difficult 
however especially wrt. threading, since you must be sure to never release the 
SCPreferences object while it is still in use.

Or you can use SCDynamicStore. Since SCDynamicStore is dynamic (hence the name) 
it won't have this problem. Since you are only interested in the location 
change notifications, SCDynamicStore is fine.
Use the following APIs (implementation left as an exercise to the user):
        SCDynamicStoreCreate[WithOptions]()
        SCDynamicStoreKeyCreateLocation(), and create an array with that, and 
pass the array to
        SCDynamicStoreSetNotificationKeys()
        SCDynamicStoreSetDispatchQueue OR 
SCDynamicStoreCopyRunLoopSource()/CFRunLoopAddRunLoopSource()

and in your callback function that was passed to SCDynamicStoreCreate, call 
SCDynamicStoreCopyLocation() to retrieve the current location. I'm sure there's 
code out there that more fully implements what I am talking about.

Ryan

> Something like:
> 
> - (void)prefsDidChange
> {
>    // reread your view of the world
> }
> 
> static void PrefsDidChange(
>    SCPreferencesRef            prefs,
>    SCPreferencesNotification     notificationType,
>    void *                      info
> )
> {
>    MyClass *   obj;
> 
>    assert(prefs != NULL);
>    if (notificationType & kSCPreferencesNotificationApply) {
>        obj = (MyClass *) info;
>        assert([obj isKindOfClass:[MyClass class]]);
> 
>        [obj prefsDidChange];
>    }
> }
> 
> - (void)someMethod
> {
>    SCPreferencesRef        prefs;
>    Boolean                 success;
>    SCPreferencesContext    context = { 0, self, NULL, NULL, NULL };
> 
>    prefs = SCPreferencesCreate(NULL, CFSTR("MyAppName"), NULL);
>    assert(prefs != NULL);
> 
>    success = SCPreferencesSetCallback(prefs, PrefsDidChange, &context);
>    assert(success);
> 
>    success = SCPreferencesScheduleWithRunLoop(
>        prefs, 
>        CFRunLoopGetCurrent(), 
>        kCFRunLoopDefaultMode
>    );
>    assert(success);
> }
> 
> 


_______________________________________________

Cocoa-dev mailing list ([email protected])

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to