Hi all,
I’m not sure whether what I’m doing is right here. I’m trying to implement a
simple logging system, where I write to a log file, and a NSTextView shows
what’s in the log.
I have a NSFileHandle that I create using +[NSFileHande
fileHandleForUpdatingAtURL:error:]
I write new log messages to it using -writeData:, and that works - I can see
that the file itself has been updated with the new information.
In order to read the content of the file, I have set up a readability handler
like this:
mLogFile.readabilityHandler = ^(NSFileHandle*
handle)
{
NSData* available = [handle
availableData];
NSString* newText = [[NSString alloc]
initWithData:available encoding:NSUTF8StringEncoding];
[self
performSelectorOnMainThread:@selector(appendLogText:) withObject:newText
waitUntilDone:NO];
[newText release];
};
I do this rather than directly updating the log text view when I write the data
so that in theory I could have other stuff writing stuff to the same file. This
block does get called when the file is updated, but -availableData is always
zero bytes. Reading the documentation, NSFileHandle seems to have only one file
offset, which is common to both read and write, which is a bit puzzling,
because surely you’d want to write at the end of the file, but read from
wherever you’d read from last time. This single offset explains why I always
get zero bytes, but it seems like a strange implementation. Have I understood
it correctly? Can I achieve what I want, or do I have to save off the previous
file offset? If I do that, I may as well write the text directly when I update
the file. This isn’t really what I want, though could probably live with it for
now.
—Graham
_______________________________________________
Cocoa-dev mailing list ([email protected])
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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [email protected]