Re: Reading data from an NSTask
Re: Reading data from an NSTask
- Subject: Re: Reading data from an NSTask
- From: Pontus Ilbring <email@hidden>
- Date: Mon, 4 Jul 2005 21:58:13 +0200
On 7/4/05, Ken Tozier <email@hidden> wrote:
> Hi
>
> I created an NSTask to run nibtool and am seeing the result in the
> run log, but so far haven't had any luck at pulling the data out of
> the task. Anyone see the problem in the below snippet?
>
> NSTask *testTask1 = [NSTask launchedTaskWithLaunchPath: @"/
> usr/bin/nibtool"
> arguments: [NSArray
> arrayWithObjects: @"-a", @"/Users/kentozier/Desktop/TestNib.nib", nil]];
>
> // if I step through in the debugger, it hangs on the next line.
> // The run log however shows that the nib data exists somewhere as it
> get printed.
> NSData *test1Data = [[testTask1 standardOutput]
> readDataToEndOfFile];
>
> NSString *testString = [[NSString alloc] initWithData: test1Data
> encoding: NSUnicodeStringEncoding];
>
> // the code never seems to make it to here
> NSLog(@"standard output = %@", testString);
I'll try to explain what goes wrong here. First, you do not allocate a
new pipe for the subtask's stdout, so it inherits the stdout of your
program. This is the problem.
Now all your important data is going into your program's stdout, which
means to the run log, which means it is lost because data can only be
read from a stream once. So your readDataToEndOfFile won't find any
data to read, and won't stop trying to read until it actually finds an
end of file marker.
But pipes don't send end of file until they are closed by -all-
processes where they are open for writing, and your pipe is open for
writing in both the subtask and your main program. Now the subtask
closes the pipe as soon as it is done, but your main program won't
close the pipe until it has quit. And it never will quit, because it
is stuck waiting for the pipe to close.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden