On Fri, Oct 17, 2008 at 4:14 PM, Karl Moskowski
<[EMAIL PROTECTED]> wrote:
>        LSSharedFileListRef list =
> LSSharedFileListCreate(kCFAllocatorDefault,
> kLSSharedFileListSessionLoginItems, nil);
>        LSSharedFileListAddObserver(list, CFRunLoopGetCurrent(),
> kCFRunLoopDefaultMode, &LoginItemsListChanged, nil);
>        if (list) CFRelease(list);

Are you sure you want to release the list?  The documentation isn't
clear on whether you should release the list immediately, but I'm
reasonably certain that you shouldn't.

Also, a couple of style issues:

1) nil is for objects (type id), NULL is for C pointers.  They compile
down to the same thing, but they're contextually different.  Use NULL
instead.
2) You don't check the return value of LSSharedFileListCreate before using it.
3) If you're assigning a function pointer, you don't actually need to
prefix the function name with the address-of operator.  The compiler
knows that all you can do with a function is call it or take its
address, so it allows you to unclutter your code a bit.
4) You're using CFRunLoopGetCurrent().  Are you sure your -init method
will only be called in the correct runloop?  Since you're in a Cocoa
program, why aren't you using the main NSRunLoop?  Check out
-[NSRunLoop getCFRunLoop].
5) I don't know if you have any state information that's important to
your callback, but if you do, you can use the context argument to pass
self to the callback, effectively turning it into a trampoline.

I'd write your method like this:

@interface MyObject
{
  LSSharedFileListRef loginItemsList;
}
@end

@implementation MyObject

-(id)init
{
  self = [super init];
  if(!self)
    return nil;

  loginItemsList = LSSharedFileListCreate(kCFAllocatorDefault,
    kLSSharedFileListSessionLoginItems, NULL);
  if(!loginItemsList)
  {
    // Ack!
    @throw [NSException
exceptionWithName:@"LSSharedFileListCreateFailedException"
      reason:@"Could not create shared file list" userInfo:nil];

    // Or you could just do this:
    // [self release];
    // return nil;
  }

  LSSharedFileListAddObserver(loginItemsList,
    [[NSRunLoop mainRunLoop] getCFRunLoop],
    kCFRunLoopDefaultMode, LoginItemsChanged, self);
}

- (void)dealloc
{
  if(loginItemsList)
  {
    LSSharedFileListRemoveObserver(loginItemsList,
      [[NSRunLoop mainRunLoop] getCFRunLoop],
      kCFRunLoopDefaultMode, LoginItemsChanged, self);

    CFRelease(loginItemsList);
  }

  [super dealloc];
}

static void LoginItemsChanged(LSSharedFileListRef list, void *context)
{
  // I'm being general here.  This would be the pattern I'd adopt if I were
  // monitoring multiple shared files lists.
  if(list == loginItemsList)
    [(id)context loginItemsChanged:list];
}

- (void)loginItemsChanged:(LSSharedFileListRef)list
{
  UInt32 seed;
  NSArray *items = (NSArray *)LSSharedFileListCopySnapshot(list, &seed);
  NSLog(@"Seed: %u, Items: %@", seed, items);

  [items release];
}

@end

Please be aware I typed this code in the Compose window.  It probably
won't compile, much less work.

--Kyle Sluder
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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