I'm attempting to read data from the Standard Error of a NSTask
in Cocoa using waitForDataInBackgroundAndNotify
. The following code does read the stream, so it's already working partially.
The problem I have is that sometimes the NSFileHandleDataAvailableNotification
starts firing repeatedly (thousands of times per second) with no new data at all ([data length]
returns 0
). My process then starts using a lot of CPU, slowing the machine to a halt. Has any of you guys hit something like this before in the past? Thanks in advance.
/**
* Start reading from STDERR
*
* @private
*/
- (void)startReadingStandardError {
NSFileHandle *fileHandle = [_task.standardError fileHandleForReading];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(errorData:)
name:NSFileHandleDataAvailableNotification
object:fileHandle];
[fileHandle waitForDataInBackgroundAndNotify];
}
/**
* Fired whenever new data becomes available on STDERR
*
* @private
*/
-(void) errorData: (NSNotification *) notification
{
NSFileHandle *fileHandle = (NSFileHandle*) [notification object];
NSData *data = [fileHandle availableData];
if ([data length]) {
// consume data
}
[fileHandle waitForDataInBackgroundAndNotify];
}