1

A question for someone who speaks both Swift and Xojo…

As shell objects in Xojo can be run both synchronously and asynchronously, how can a Swift Process() be run the same way?

With Xojo I can run a shell and respond accordingly when data is received from the process being run, thus not locking up the UI during long processes.

With Swift I only know how to run a Process() and handle the resulting data in one lump.

Can anyone who is familiar with Swift Process() please offer me any insights?

psb
  • 89
  • 5
  • 2
    There is https://developer.apple.com/documentation/foundation/filehandle/1409270-waitfordatainbackgroundandnotify on the `Pipe()`. You can get the data and then call your closure. – Larme Apr 14 '21 at 15:51
  • I do believe this is what I'm looking for. I will read up, experiment and try to figure out how to implement. (I'm very new to Swift) Thanks for pointing me in the right direction! – psb Apr 14 '21 at 19:56

1 Answers1

0

In swift you can run the process in the dispatch queue async.

DispatchQueue.main.async {
  // your task here
}

DispatchQueue.global(qos: .userInitiated).async {
 // your task here
}

DispatchQueue.global(qos: .background).async {
 // your task here
}

In your case I would recommend searching on GitHub for swift "shell" packages.
If you don't want to use packages you will find a lot of examples when searching for DispatchQueue async.
The best to start with:
https://www.hackingwithswift.com/read/9/4/back-to-the-main-thread-dispatchqueuemain

DoTryCatch
  • 1,052
  • 6
  • 17
  • I don't believe this is related to the question. The issue is how to manage external processes (on Mac), not internal dispatch queues. See psb's comments about Pipe for how to do what is being asked for. Turning psb's comment into a full answer (after some research) would be valuable, however. See the docs for Process to get started. https://developer.apple.com/documentation/foundation/process# – Rob Napier Jun 27 '21 at 21:54