NSTask waitForDataInBackgroundAndNotify and waitUntilExit
NSTask waitForDataInBackgroundAndNotify and waitUntilExit
- Subject: NSTask waitForDataInBackgroundAndNotify and waitUntilExit
- From: Torsten Curdt <email@hidden>
- Date: Mon, 12 May 2008 22:57:20 +0200
I am trying to make use of "waitForDataInBackgroundAndNotify" when
executing a script. I've setup the task like this
NSTask *task = [[NSTask alloc] init];
[task setLaunchPath:path];
[task setArguments:args];
NSPipe *outPipe = [NSPipe pipe];
[task setStandardOutput:outPipe];
NSFileHandle *outFile = [outPipe fileHandleForReading];
NSPipe *errPipe = [NSPipe pipe];
[task setStandardError:errPipe];
NSFileHandle *errFile = [errPipe fileHandleForReading];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(errData:)
name:NSFileHandleDataAvailableNotification
object:errFile];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(outData:)
name:NSFileHandleDataAvailableNotification
object:outFile];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(terminated:)
name:NSTaskDidTerminateNotification
object:task];
[outFile waitForDataInBackgroundAndNotify];
[errFile waitForDataInBackgroundAndNotify];
NSLog(@"launching command");
[task launch];
NSLog(@"waiting for command to finsh");
[task waitUntilExit];
and while the I get and handle the "outData" call.
-(void) outData: (NSNotification *) notification
{
NSFileHandle *fileHandle = (NSFileHandle*) [notification
object];
NSLog(@"received out data");
NSData *data = [fileHandle availableData];
if ([data length]) {
NSLog(@"appending out data");
[output appendString:[NSString stringWithCString:[data
bytes] encoding: NSUTF8StringEncoding]];
}
[fileHandle waitForDataInBackgroundAndNotify];
NSLog(@"receiving out done");
}
There is seems to be no NSTaskDidTerminateNotification and the task is
just sits there and waits
launching command
waiting for command to finsh
received out data
appending out data
receiving out done
While there is a lot to find about NSTask and waitUntilExit on the net
I could not find anything when waitForDataInBackgroundAndNotify is used.
Pointers what I am doing wrong?
cheers
--
Torsten
_______________________________________________
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