Hello, I need to develop a tool which processes stdin as it arrives from its parent process. The following code works as expected when it is in invoked with no stdin, that is…
Air2:Debug jk$ ./MyTool But if I pipe some initial stdin to it, like this Air2:Debug jk$ echo Hello | ./MyTool after the initial notification and processing of “Hello”, the notification-handling block infinitely receives repeated, different notifications, for which -availableData returns an empty data object. I can’t understand and cannot work around it. Why am I getting infinitely repeated notifications when there is no data available? Thanks! Jerry int main(int argc, const char * argv[]) { @autoreleasepool { /* -[NSFileManager waitForDataInBackgroundAndNotify] docs say I need an "active run loop". I don't know what they mean by "active". Maybe this will help (it didn't). */ NSDate* verySoon = [NSDate dateWithTimeIntervalSinceNow:0.01] ; [[NSRunLoop mainRunLoop] runUntilDate:verySoon] ; NSFileHandle* input = [NSFileHandle fileHandleWithStandardInput] ; [input waitForDataInBackgroundAndNotify] ; NSNotificationCenter* dc = [NSNotificationCenter defaultCenter] ; [dc addObserverForName:NSFileHandleDataAvailableNotification object:input queue:nil usingBlock:^(NSNotification* note) { NSFileHandle* input ; input = [NSFileHandle fileHandleWithStandardInput] ; NSData* data = [input availableData] ; NSLog(@"Got noted of %ld bytes", (long)[data length]) ; if ([data length] > 0) { NSFileHandle* output ; output = [NSFileHandle fileHandleWithStandardOutput] ; [output writeData:[NSData dataWithBytes:"--> " length:4]] ; [output writeData:data] ; } [input waitForDataInBackgroundAndNotify] ; }] ; /*SSYDBL*/ NSLog(@"Running run loop") ; [[NSRunLoop mainRunLoop] run] ; } return 0 ; } _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) Please do not post admin requests or moderator comments to the list. Contact the moderators at cocoa-dev-admins(at)lists.apple.com Help/Unsubscribe/Update your Subscription: https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com