Domanda

Trying to read the data returned from an NSTask causes a hang that never returns. I've verified my script being run does in fact return data to both stdout and stderr. It's a simple two line shell script that sends one line to stdout and the other to stderr.

The NSLog output says

Got handle: <NSConcreteFileHandle: 0x10010a800>

And then it just hangs. This is the code I'm using.

    NSPipe *stderr = [NSPipe pipe];
    NSPipe *stdout = [NSPipe pipe];

    NSTask *task = [[NSTask alloc] init];
    task.standardError = stderr;
    task.standardOutput = stdout;
    task.standardInput = [NSPipe pipe];
    task.launchPath = @"/tmp/f";

    [task launchPath];
    [task waitUntilExit];

    NSFileHandle *fh = [stderr fileHandleForReading];

    NSLog(@"Got handle: %@", fh);

    [fh readDataToEndOfFile];

    NSLog(@"Read it");
È stato utile?

Soluzione

It's because you have never actually launched your task. You call

[task launchPath];

That just returns the task's path as a string, it doesn't actually launch the task. You want

[task launch];

Altri suggerimenti

Tom is right, but also you need to launch the task before trying to envoke readDataToEndOfFile

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top