NSTask die Echtzeit-Ausgabe

Ich habe ein PHP-Skript, welches mehrere sleep() Befehle. Ich möchte, um es auszuführen in meiner Anwendung mit NSTask. Mein Skript sieht wie folgt aus:

echo "first\n"; sleep(1); echo "second\n"; sleep(1); echo "third\n";

Ich ausführen kann, meine Aufgabe asynchron mit Benachrichtigungen:

- (void)awakeFromNib {
    NSTask *task = [[NSTask alloc] init];
    [task setLaunchPath: @"/usr/bin/php"];

    NSArray *arguments;
    arguments = [NSArray arrayWithObjects: @"-r", @"echo \"first\n\"; sleep(1); echo \"second\n\"; sleep(1); echo \"third\n\";", nil];
    [task setArguments: arguments];

    NSPipe *p = [NSPipe pipe];
    [task setStandardOutput:p];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskExited:) name:NSTaskDidTerminateNotification object:task];

    [task launch];

}

- (void)taskExited:(NSNotification *)notif {
    NSTask *task = [notif object];
    NSData *data = [[[task standardOutput] fileHandleForReading] readDataToEndOfFile];
    NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
    NSLog(@"%@",str);
}

Meiner Ausgabe (nach 2 Sekunden, natürlich):

2011-08-03 20:45:19.474 MyApp[3737:903] first
second
third

Meine Frage ist: wie bekomme ich diese drei Worte sofort, nachdem Sie gedruckt sind?

InformationsquelleAutor akashivskyy | 2011-08-03
Schreibe einen Kommentar