Monitoring Files With GCD Being Edited With A Text Editor

Posted by David Hamrick on

I recently posted about monitoring files with GCD and this approach works very well. I was using this in an iOS application and would edit the configuration file with TextMate and whenever I save it, it would dynamically reload the file. However whenever I opened the file with Vim or XCode the file would receive the notification the first time and then not again after that.

Certain editors appear to be deleting the file and then re-saving it. Or at the very least I am receiving the DISPATCH_VNODE_DELETE flag set in dispatch source event handler. So in order to have the desired behavior of monitoring the file continuously I had to modify my event handler a bit differently.

This method listens for events on that file descriptor and when the file is deleted it calls itself again, setting up a new event handler. This solves the problem that certain editors were causing by deleting the files before saving them.

Grand Central Dispatch dispatch source flags

It was not immediately obvious to me how to see which flags were set on a dispatch source event. But after asking a question on Stack Overflow I found the answer in apple’s documentation in the Concurrency Programming Guide. All you need to do is call the dispatch_source_get_data function and it will return the flags that are set. Here is the relevant line from the guide:

For a descriptor dispatch source that monitors file system activity, this function returns a constant indicating the type of event that occurred. For a list of constants, see the dispatch_source_vnode_flags_t enumerated type.


David Hamrick is a partner at Hamrick Software, the makers of VueScan, the worlds most popular 3rd party scanner software.