Hi, I'm trying to execute a task in the background and parsing the output from the task along the way. However I get the NSTaskDidTerminateNotification before all the output from the task has been delivered by NSFileHandleReadCompletionNotification - and I am not able to squeeze much more (but in some cases a little, but never all the way to the end) out of the filehandle after the task exits.
Code is what we all want to look at, so here are the interesting bits. <code start> App.h: NSTask *myTask; NSFileHandle *myFileHandle; App.m: - (id) init { [super init]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(threadPipeReader:) name:NSFileHandleReadCompletionNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(threadTaskStopped:) name:NSTaskDidTerminateNotification object:nil]; return self; } -(void)startMyTask { NSPipe *pipe = [[NSPipe alloc] init]; myFileHandle = [pipe fileHandleForReading]; [myFileHandle readInBackgroundAndNotify]; myTask = [[NSTask alloc] init]; [myTask setLaunchPath:launchPath]; [myTask setCurrentDirectoryPath:homeDirectory]; [myTask setStandardOutput: pipe]; [myTask setStandardError: pipe]; [myTask setArguments:arguments]; [myTask launch]; } -(void)threadPipeReader:(NSNotification *)notification { NSData *data; data = [[notification userInfo] objectForKey:NSFileHandleNotificationDataItem]; [ ... do something with data ... ] [[notification object] readInBackgroundAndNotify]; } -(void)threadTaskStopped:(NSNotification *)notification { NSData *data = [myFileHandle availableData]; while ([data length] > 0) { NSLog(@"got some more: %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); NSLog(@"output size: %d", [data length]); data = [myFileHandle availableData]; } } <code end> I never got the full output in threadPipeReader, but then I tried to fetch the data in threadTaskStopped - but that only gives some output. Not all the way to the end either. I then tried to specify how much data I wanted from myFileHandle in threadTaskStopped, by doing: NSData *data = [myFilehandle readDataOfLength:262144]; And then got the output: got some more: <about 262144 characters of data> output size: 262144 But then the while-loop exited because myFileHandle was zero length the next time it got polled. When I don't specify a size, but just asks for availableData, I get sizes of exactly 16K (16384) multiple times. But never an odd size - and I can definetely see that the output gets chopped off (sometimes mid-line) - so I am never able to get the last output from the task. Can someone help me with this? Best regards Rasmus Skaarup _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com