Post marked as solved
Click to stop watching this thread.
You have stopped watching this post. Click to start watching again.
contentPostList.repliessolved.tooltip
I did flush the stdout by adding this line-> setvbuf(stdout, nil, IONBF, 0)
But still nothing display on the text view. Only happens in release mode.
func displayOutput ( task:Process) {
let outputPipe = Pipe()
setvbuf(stdout, nil, IONBF, 0)
dup2(outputPipe.fileHandleForReading.fileDescriptor, STDOUTFILENO)
task.standardOutput = outputPipe
outputPipe.fileHandleForReading.waitForDataInBackgroundAndNotify()
NotificationCenter.default.addObserver(forName: NSNotification.Name.NSFileHandleDataAvailable, object: outputPipe.fileHandleForReading , queue: nil) { notification in
let output = outputPipe.fileHandleForReading.availableData
let outputString = String(data: output, encoding: String.Encoding.utf8) ?? ""
if outputString.count > 0 {
DispatchQueue.main.async(execute: {
let previousOutput = self.outputTextView.string
let nextOutput = previousOutput + "\n" + outputString
self.outputTextView.string = nextOutput
let range = NSRange(location:nextOutput.count,length:0)
self.outputTextView.scrollRangeToVisible(range)
})
}
outputPipe.fileHandleForReading.waitForDataInBackgroundAndNotify()
}
}