Can't get folder monitoring working with GCD and dispatch_source_create

I'm trying to monitor my iTunes file sharing folder for changes. I've seen several posts over the years describing how to best do this (it's unfathomable that Apple STILL doesn't provide an easy way to do this). This solution seems to be the most currently-accepted way, using GCD. Except that it doesn't seem to work. I have this code in my app delegate's application:didFinishLaunchingWithOptions function, but when I add a file via iTunes file sharing, the event handler doesn't get called. I'm definitely getting the proper folder path for the directory. The file descriptor I get from calling open() is 5, which seems curious, but at least it's not -1, which is the value it would be if it couldn't find the path and open the location. Any ideas? Btw, this new forum setup so far seems awful.



NSArray* docDirPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString* fileSharingDirNS = [docDirPaths objectAtIndex:0];

uintptr_t fileDescriptor = open([fileSharingDirNS fileSystemRepresentation], O_EVTONLY);

dispatch_source_t dispatchSource = dispatch_source_create(
  DISPATCH_SOURCE_TYPE_VNODE,
  fileDescriptor,
  DISPATCH_VNODE_DELETE | DISPATCH_VNODE_WRITE | DISPATCH_VNODE_EXTEND | DISPATCH_VNODE_RENAME | DISPATCH_VNODE_REVOKE,
  dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));

dispatch_source_set_event_handler(dispatchSource, ^{
  printf("itunes file sharing files modified\n");
  });
dispatch_source_set_cancel_handler(dispatchSource, ^{
  close(fileDescriptor);
  });

dispatch_resume(dispatchSource);

Replies

I'm not 100% sure what's going on with your code but there are a bunch of examples of using DISPATCH_SOURCE_TYPE_VNODE in the iOS Developer Library; search the library for "DISPATCH_SOURCE_TYPE_VNODE" and follow the code from there.


Share and Enjoy

--

Quinn "The Eskimo!"

Apple Developer Relations, Developer Technical Support, Core OS/Hardware

It is most likely that nothing keeps strong reference to the dispatch_source so it gets released after your code executes.