• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Problem with reading an NSPipe->NSFileHandle to end
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Problem with reading an NSPipe->NSFileHandle to end


  • Subject: Problem with reading an NSPipe->NSFileHandle to end
  • From: Rasmus Skaarup <email@hidden>
  • Date: Thu, 8 Apr 2010 16:57:04 +0200

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 (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

  • Follow-Ups:
    • Re: Problem with reading an NSPipe->NSFileHandle to end
      • From: Jens Alfke <email@hidden>
    • Re: Problem with reading an NSPipe->NSFileHandle to end
      • From: Ken Thomases <email@hidden>
  • Prev by Date: nstableview in drawer resetting custom cursor
  • Next by Date: Table with cells as dragging destination?
  • Previous by thread: Re: nstableview in drawer resetting custom cursor
  • Next by thread: Re: Problem with reading an NSPipe->NSFileHandle to end
  • Index(es):
    • Date
    • Thread