1

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 this VDKQueue *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
Community
  • 1
  • 1
urbanrider
  • 231
  • 5
  • 14
  • 1
    That's what the last line tells you: "do your business _in the delegate method_". The delegate method is your callback. – jscs Jan 12 '13 at 21:29
  • do you mind expnding on that? I dont quite understand. I updated the points above with the code I have – urbanrider Jan 12 '13 at 23:29

1 Answers1

3

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 VDKQueueDelegate protocol. If you're not already doing this, you should be getting a warning about it, since setDelegate: 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.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Ive never used NSWorkspace or delegates really, which one is easier to implement for a beginner? (Delegates look a little daunting) – urbanrider Jan 12 '13 at 22:04
  • @urbanrider: The only involvement of NSWorkspace is to get the notification center; from there on, it's all notification center logic. The delegate method is actually easier. – Peter Hosey Jan 12 '13 at 22:05
  • Ok so added `@interface AppDelegate : NSObject {` to my AppDelegate.h file, which removed the warning from `[kqueue setDelegate:self];` I still don't understand where to put the code that I want to run when the file is updated? – urbanrider Jan 12 '13 at 22:17
  • @urbanrider: In your implementation of the delegate method. – Peter Hosey Jan 13 '13 at 00:24
  • ok I "think" I under stand the delegate methode. I added a piece of code from the header. Is that the delegate code I need to make? – urbanrider Jan 13 '13 at 00:29
  • @urbanrider: Without knowing what code you added and where, probably not. You need to implement the method that is required by the `VDKQueueDelegate` protocol, in the object that you are setting as the VDKQueue's delegate. In that method, you do whatever you need to do when a watched file changes. – Peter Hosey Jan 13 '13 at 00:30
  • If I add `-(void) VDKQueue:(VDKQueue *)queue receivedNotification:(NSString*)noteName forPath:(NSString*)fpath{ ... }` it says Unexpected interface name 'VDKQueue': expected expression – urbanrider Jan 13 '13 at 00:31
  • @urbanrider: It sounds like you pasted that within another method. You can't have a method inside of a method. See the “Programming with Objective-C” document for info on where methods can go: http://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/ Also, “Cocoa Core Competencies” on delegation: http://developer.apple.com/library/mac/documentation/General/Conceptual/DevPedia-CocoaCore/Delegation.html – Peter Hosey Jan 13 '13 at 00:33
  • Ahhh thanks im an idiot!!! It seems to work now haha....one last thing in the bit of coded I added to my original post, it talks about the "required". Do I need to do anything with that? – urbanrider Jan 13 '13 at 00:42
  • 1
    @urbanrider: `@required` means you need to implement that method if you want to conform to the protocol. If you declare conformance (`@interface MyClass : … `) but don't implement all required methods, the compiler will warn you about that, and your program will probably crash. Implementing all required methods is all you need to do to satisfy the requirement. – Peter Hosey Jan 13 '13 at 00:55
  • ahh ok thanks. Last question (I Hope haha) it will detect a change and I can get information about it etc. but it seems to stop after the first notification. Do I need to restart the listener or something? Im assuming I don't need to re-initiate all the stuff again? – urbanrider Jan 13 '13 at 02:35
  • @urbanrider: I don't know. Check the VDKQueue documentation, including its header. – Peter Hosey Jan 13 '13 at 02:36