2012-04-01 3 views
1

그래서,이 내 코드입니다 ph test.php).비동기 실행은 제대로

  • 스크립트는
  • 잘 실행하지만, 나는 모든 출력을 얻을 관리했습니다 전에 나는 Data ReadyTask Terminated 알림을 수신하고 있습니다 :

    것은입니다.

나는 기본적으로 비동기 읽기 싶은 (내 말은, dataReady: 기능 ...을
출력과 그것의 나머지 단지 첫 번째 부분은 어디에서도 발견 할 수있다 페치), 모든 출력 - 명령이 실행 중일 때.

어떤 아이디어? 내가 도대체 ​​뭘 잘못하고있는 겁니까?

감사합니다.

답변

4

readInBackgroundAndNotify을 사용하여 독서 일정을 예약하십시오. 이 메서드는 데이터로 가득 찬 하나의 버퍼 만 읽고이를 알립니다. 더 많은 데이터를 읽으려면 알림 방법에서 readInBackgroundAndNotify으로 다시 전화해야하며 모든 데이터를 즉시 수신하려면 readToEndOfFileInBackgroundAndNotify을 사용해야합니다.

+0

확실히 맞습니다. (그냥 나 자신을 알아 냈어, 근데, 어쨌든 많이 고마워 !-)) –

1

10.7 이후 새 API가 있으므로 NSNotifications를 사용하지 않아도됩니다.

task.standardOutput = [NSPipe pipe]; 
[[task.standardOutput fileHandleForReading] setReadabilityHandler:^(NSFileHandle *file) { 
    NSData *data = [file availableData]; // this will read to EOF, so call only once 
    NSLog(@"Task output! %@", [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]); 

    // if you're collecting the whole output of a task, you may store it on a property 
    [self.taskOutput appendData:data]; 
}]; 

중요 :

귀하의 작업, 당신은 전무에 readabilityHandler 블록을 설정해야 종료

; 그렇지 않으면 읽기가 중단되지 않으므로 높은 CPU 사용량이 발생합니다.

[task setTerminationHandler:^(NSTask *task) { 

    // do your stuff on completion 

    [task.standardOutput fileHandleForReading].readabilityHandler = nil; 
    [task.standardError fileHandleForReading].readabilityHandler = nil; 
}];