MBFileEvents
Version 1.0
Download
MBFileEvents is a category on NSString which allows you to register to receive notifications when a file is modified. It provides an extremely light-weight interface wrapped around a kqueue/kevent implementation.
You can add a file observer with a single line of code:
Provided that string is a valid path and there was no error opening the file, this method will return YES. If this is the first time you are adding a file observer, a kqueue will be created and a background thread will be spun off to monitor the queue for events.
When an event is detected the observer will receive the following callback, declared as an informal protocol:
The event is simply a bitfield, and can be masked to determine the components of the event. For example:
Note that you are monitoring the file itself, not the path, so once the file is deleted you won't receive any further callbacks. If you anticipate that the file has been replaced by a new file at the same path, you must add the observer again to begin monitoring the new file.
To remove an observer, simply call the following:
This isn't necessary if you are using garbage collection.
MBFileEvents is released under the modified BSD license, with no warranty of any kind. Leopard is required, I'm sorry to say, because I'm using the new NSPointerArray class for weak referencing of observers under garbage collection.
Download
MBFileEvents is a category on NSString which allows you to register to receive notifications when a file is modified. It provides an extremely light-weight interface wrapped around a kqueue/kevent implementation.
You can add a file observer with a single line of code:
BOOL success = [string addFileEventsObserver:self];Provided that string is a valid path and there was no error opening the file, this method will return YES. If this is the first time you are adding a file observer, a kqueue will be created and a background thread will be spun off to monitor the queue for events.
When an event is detected the observer will receive the following callback, declared as an informal protocol:
- (void)observeFileEvent:(MBFileEvent)event forFilePath:(NSString *)path;The event is simply a bitfield, and can be masked to determine the components of the event. For example:
if ((event & MBDeleteFileEventMask) == MBDeleteFileEventMask)
DLog(@"File at path '%@' was deleted", path);Note that you are monitoring the file itself, not the path, so once the file is deleted you won't receive any further callbacks. If you anticipate that the file has been replaced by a new file at the same path, you must add the observer again to begin monitoring the new file.
To remove an observer, simply call the following:
[string removeFileEventsObserver:self];This isn't necessary if you are using garbage collection.
MBFileEvents is released under the modified BSD license, with no warranty of any kind. Leopard is required, I'm sorry to say, because I'm using the new NSPointerArray class for weak referencing of observers under garbage collection.