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.
1 Comments:
As a long time Cocoa developer I'd like to suggest that instead of using a category on NSString that you create your own class that takes an observer and path as arguments.
Just seems to be clearer than adding a category to NSString where only strings that represent paths work. What if later you wanted to add support for aliases or something.. they aren't strings and it would be odd to add another string category method that took an alias and observer.
Just my 2 cents.
Post a Comment
<< Home