Allow me to follow up again on this one. I've been trying to find out, why this isn't working running natively physically on the target device (iPhone) for a week or so. I even sold my soul to Apple subscribing their developer program. Got a certificate and signed the app. Well it eases development now a bit anyway so that I'm not bound to these self signing of Apps.
I put up this problem to Apple developer forum with no answer yet. They take a couple of days for validate the post and since I moved it there from "Swift" to "iOS simulation and devices" it will take another while so that everyone can read it.
I opened an SO thread also which at least lead to confirmation that my posted behaviour can be reproduced.
I need this way of debugging my app (running it natively to for testing it in field) and the essence now is, that I need to debug my debug code.
public func openConsolePipe () {
// dup2(STDOUT_FILENO, outputPipe.fileHandleForWriting.fileDescriptor)
stdout_fd = Int(dup2(pipe.fileHandleForWriting.fileDescriptor, STDOUT_FILENO))
stderr_fd = Int(dup2(pipe.fileHandleForWriting.fileDescriptor, STDERR_FILENO))
// listening on the readabilityHandler
pipe.fileHandleForReading.readabilityHandler = { [weak self] handle in
let data = handle.availableData
let str = String(data: data, encoding: .ascii) ?? "<Non-ascii data of size\(data.count)>\n"
DispatchQueue.main.async {
self?.textView.text += str
}
}
}
Either of pipe() or DispatchQueue.main.async or something else is not working.
I appended the result of stdout_fd and stderr_fd to the textView.text (the only way of debugging at the moment when running standalone) and their values are 1 and 2 resp..
How can I test whether the code block "listening on the readabilityHandler" returns something meaningful?
Is DispatchQueue.main.async requiring GCD? And would the latter be running in standalone mode or would DispatchQueue.main.async require some initialization in native mode?
What type is "readabilityhandler" ? Can I assign it to something and test it for nil? Or can I guard the code block to see whether is different behaviour in standalone vs. Xcode debugging?
You write "Be sure not to let pipe get destroyed." Would it be kept alive when placed in the scope of ViewController?
And a question regarding the code piece:
// listening on the readabilityHandler
pipe.fileHandleForReading.readabilityHandler = { ... }
means, a code section (in the curly braces) is established to be the "readabilityHandler" for that fileHandle, right?