I’ve been looking around and it looks like VDKQueue is a more modern version of UKKQueue, but I’m having trouble implementing it (I’m not great at Cocoa yet).
I have this so far but I’m at a bit of a loss on what else I need (or if this is even right):
VDKQueue *kqueue = [[VDKQueue alloc] init];
[kqueue addPath:path notifyingAbout:VDKQueueNotifyAboutWrite];
[kqueue setDelegate:self];
This answer seems to nicely outline how to set it up, I just don’t really understand it.
Now that I have VDKQueue initialized, how to I set what happens when the file is modified?
Cocoa Monitor a file for modifications
From the other answer:
Implementation was pretty straightforward:
- let your controller be the
VDKQueueDelegate; (I added<VDKQueueDelegate>to my AppDelegate.h)- declare a
VDKQueue*ivar / property; (Is thisVDKQueue *kqueue = [[VDKQueue alloc] init];?)- setup delegate method
VDKQueue:receivedNotification:forPath:; (How do I do this?)- init the queue and set its delegate to the controller itself; (is this this
[kqueue setDelegate:self];?)- add resources to watch with
addPath:notifyingAbout:. (Added this line[kqueue addPath:path notifyingAbout:VDKQueueNotifyAboutWrite];)Then just do your business in the delegate method.
Possibly the delegate method from the code?
//
// Or, instead of subscribing to notifications, you can specify a delegate and implement this method to respond to kQueue events.
// Note the required statement! For speed, this class does not check to make sure the delegate implements this method. (When I say "required" I mean it!)
//
@class VDKQueue;
@protocol VDKQueueDelegate <NSObject>
@required
-(void) VDKQueue:(VDKQueue *)queue receivedNotification:(NSString*)noteName forPath:(NSString*)fpath;
@end
There are a couple of ways, both of them documented in the VDKQueue header file.
Method A: Notifications
Add an observer on the NSWorkspace’s notification center for the various VDKQueue notifications, listed in that header file. The notification center will call your block (or send a message to your own observer object, if you use that older but still perfectly valid method) when the VDKQueue sends a notification you’re observing for.
Method B: Delegate
You’re already setting yourself as the delegate, which is one of the steps.
Step 1 is to declare that you conform to the
VDKQueueDelegateprotocol. If you’re not already doing this, you should be getting a warning about it, sincesetDelegate:requires an object that conforms to the protocol.Step 2 is to fulfill that promise by actually implementing all of the required methods of the protocol. There’s currently only one.
Step 3 is to set yourself as the delegate.
In your implementation of
VDKQueue:receivedNotification:forPath:, which is the method you implemented in step 2, you do whatever you want to do to react to what just happened to the file.