Process() object and async operation

I am maintaining a macOS app, a GUI on top of a command line tool. A Process() object is used to kick off the command line tool with arguments. And completion handlers are triggered for post actions when command line tool is completed. My question is: I want to refactor the process to use async and await, and not use completion handlers.

func execute(command: String, arguments:[String]) -> async {

let task = Process()
task.launchPath = command
task.arguments = arguments
...
do {
    try task.run()
 } catch let e {
    let error = e
    propogateerror(error: error)
 }
...
}
...

and like this in calling the process

await execute(..)

Combine is used to monitor the ProcessTermination:

NotificationCenter.default.publisher(
            for: Process.didTerminateNotification)
            .debounce(for: .milliseconds(500), scheduler: DispatchQueue.main)
            .sink { _ in
              .....
                // Release Combine subscribers
                self.subscriptons.removeAll()
            }.store(in: &subscriptons)

Using Combine works fine by using completion handlers, but not by refactor to async. What is the best way to refactor the function? I know there is a task.waitUntilExit(), but is this 100% bulletproof? Will it always wait until external task is completed?

I’m not sure I fully understand your question, but I think you’re looking for Running a Child Process with Standard Input and Output. If that doesn’t help, please reply back to clarify your goals.

ps This will all be a lot nicer once Subprocess lands.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Hi Quinn.

Thank you very much for reply. As far as I can understand of the new Subprocess, I belive that is the answer to my question.. Looking forward to the release...

Process() object and async operation
 
 
Q