• 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: dispatch_source does not work
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: dispatch_source does not work


  • Subject: Re: dispatch_source does not work
  • From: Kyle Sluder <email@hidden>
  • Date: Wed, 05 Nov 2014 01:16:41 -0600

On Wed, Nov 5, 2014, at 12:50 AM, Gerriet M. Denkmann wrote:
> I want to monitor a file for changed content.
> Polling is generally not a good idea, so I tried dispatch_source.
>
> But it does not work.
>
> Here the code:
>
> static NSString *const kTestPath        = @"/tmp/a.test";
>
> - (void)applicationDidFinishLaunching:(NSNotification *)aNotification
> {
> 	const char *filename = kTestPath.fileSystemRepresentation;
> 	int fd = open(filename, O_RDONLY);
> 	fcntl(fd, F_SETFL, O_NONBLOCK);
> 	uintptr_t fD = (uintptr_t)fd;
> 	dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
> 	const uint64_t flags = DISPATCH_VNODE_WRITE | DISPATCH_VNODE_LINK;
> 	dispatch_source_t my_source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, fD, flags, queue);
> 	dispatch_source_set_event_handler(my_source, ^{ [self fileDidChange]; } );
> 	dispatch_resume(my_source);
> }
>
> [snip]
>
>
> What am I doing wrong? (Writing directly to kTestPath without rename()
> also does not work).
>

My own test setup, which mimics yours, only works if the file already
exists when the dispatch source is created. This makes sense, since
without O_CREAT, open() returns -1 (an invalid file descriptor).

You need to pass O_RDONLY | O_CREAT to open() to ensure that the file
exists before you start trying to monitor it for changes.

--Kyle Sluder
_______________________________________________

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


References: 
 >dispatch_source does not work (From: "Gerriet M. Denkmann" <email@hidden>)

  • Prev by Date: Re: dispatch_source does not work
  • Next by Date: Re: Editing NSOutlineView Group Item
  • Previous by thread: Re: dispatch_source does not work
  • Next by thread: Initializing Structures
  • Index(es):
    • Date
    • Thread