Re: readInBackgroundAndNotify not working?
Re: readInBackgroundAndNotify not working?
- Subject: Re: readInBackgroundAndNotify not working?
- From: "Adam R. Maxwell" <email@hidden>
- Date: Sun, 23 Aug 2009 20:12:39 -0700
On Aug 23, 2009, at 8:01 PM, Kyle Sluder wrote:
On Aug 23, 2009, at 7:42 PM, PCWiz <email@hidden> wrote:
name:@"NSFileHandleReadCompletionNotification"
NSFileHandleReadCompletionNotification is the name of a constant,
not necessarily its value.
Right, and that was my first thought, too! I've never used
NSFileHandle for this purpose, either, so wrote a quick test app to
see what would happen. It turns out that the value is the same as the
constant in this case, though, and my code below seems to work if I do
date >> /tmp/testfile.txt
in Terminal.
However, if I open the file with vi and edit/save, the notifications
stop coming in. My guess is that vi does an atomic write, so watching
the file descriptor breaks when a new file is written instead of just
appending to the old one. I've run into problems like that
previously, and IIRC you can catch that case with kqueue. Maybe
CFFileDescriptor would work also, but I haven't used it yet. You may
find that none of the above will work on certain filesystems (e.g.,
NFS), in which case you probably have to poll the filesystem.
#import <Foundation/Foundation.h>
@interface Reader : NSObject
@end
@implementation Reader
- (void)dataAvailable:(NSNotification *)aNote
{
NSLog(@"%@", aNote);
[[aNote object] readInBackgroundAndNotify];
}
@end
int main (int argc, char const *argv[])
{
NSAutoreleasePool *pool = [NSAutoreleasePool new];
system("/usr/bin/touch /tmp/testfile.txt");
Reader *reader = [Reader new];
NSFileHandle *fh = [NSFileHandle fileHandleForReadingAtPath:@"/
tmp/testfile.txt"];
[[NSNotificationCenter defaultCenter] addObserver:reader
selector:@selector(dataAvailable:)
name:NSFileHandleReadCompletionNotification object:fh];
[fh readInBackgroundAndNotify];
[[NSRunLoop currentRunLoop] addPort:[NSPort port]
forMode:NSDefaultRunLoopMode];
[[NSRunLoop currentRunLoop] run];
[pool drain];
return 0;
}
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden