I'm struggling with this one for a quite of time.
I'm communicating with other program using NSPipe
, and everything works fine except when I get notification NSFileHandleDataAvailableNotification
, I don't get all data available.
So far I figured it out that the reading part of the pipe does get notification that data is available, it just isn't completed (written) yet completely. If I read it immediately after this notification, I only get partial response. But if I set thread to sleep for example 0.1s and then read, everything is OK.
Is there any way to be sure, that I will get complete response from pipe?
So far I have:
inputPipe = [NSPipe pipe];
outputPipe = [NSPipe pipe];
fileReading = inputPipe.fileHandleForReading;
[fileReading waitForDataInBackgroundAndNotify];
fileWriting = outputPipe.fileHandleForWriting;
task = [NSTask new];
task.launchPath = ... launch path
task.arguments = @[@"arg1"];
task.standardInput = outputPipe;
task.standardOutput = inputPipe;
[task launch];
Of course notification observer is set. It's selector is:
- (void) dataAvailabe:(NSNotification *) notification {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{
[NSThread sleepForTimeInterval:0.1];
dispatch_async(dispatch_get_main_queue(), ^{
NSData *data = [fileReading availableData];
NSString * reply = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
});
});