I have an Xcode app (written in Swift, though that should be irrelevant) that starts a console application (i.e. Terminal application that is written in C++) and expects to read the output from its Standard and Error outputs. The task prints from its main thread, and also from another subthread it creates.
The task is setup as follows:
standardOutputPipe = NSPipe()
standardErrorPipe = NSPipe()
someTask = NSTask()
someTask!.launchPath = transporterPath
someTask!.standardOutput = standardOutputPipe!
someTask!.standardError = standardErrorPipe!
someTask!.standardOutput!.fileHandleForReading.readInBackgroundAndNotify()
someTask!.standardError!.fileHandleForReading.readInBackgroundAndNotify()
NSNotificationCenter.defaultCenter().addObserver(self, selector: "standardDataReceived:",
name: NSFileHandleReadCompletionNotification, object: someTask!.standardOutput!.fileHandleForReading)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "errorDataReceived:",
name: NSFileHandleReadCompletionNotification, object: someTask!.standardError!.fileHandleForReading)
The issue is that my app only receives output from the main thread (through the task's FileHandleForReading on Standard and Error output); anything printed from the task via the subthread isn't caught. If I run the task from the terminal, everything is printed to the screen without issue.
In addition to the output from the main task, how can I also catch the output from the subthread of the task? Or is it possible that this issue isn't related to the threads of the task, but with something unexpected happening with the FileReader Notifications? Thanks for any help!