Skip navigation.
 
mlNSTask waitForDataInBackgroundAndNotify and waitUntilExit
FROM : Torsten Curdt
DATE : Mon May 12 22:57:20 2008

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

Related mailsAuthorDate
mlNSTask waitForDataInBackgroundAndNotify and waitUntilExit Torsten Curdt May 12, 22:57
mlRe: NSTask waitForDataInBackgroundAndNotify and waitUntilExit Shawn Erickson May 12, 23:04
mlRe: NSTask waitForDataInBackgroundAndNotify and waitUntilExit Torsten Curdt May 13, 02:55