0

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!

P.M.
  • 436
  • 6
  • 12
  • You probably need to show the main function for your secondary thread. Are you running it's runloop? – nielsbot Nov 03 '15 at 01:08
  • Try adding some logs to make sure it's not exiting immediately? – nielsbot Nov 03 '15 at 01:09
  • @nielsbot The NSTask definitely is not exiting immediately, as I get output from its main thread up until I manually kill the task. How do I tell the main function about the secondary thread? Does NSTask obfuscate its sub-threads from its calling function (i.e. the main program)? – P.M. Nov 03 '15 at 21:05

0 Answers0