I am running a binary file using a swift process() in my DataModel class. The user can see the process running from the ViewTasks() struct. How can I give the user the ability to cancel (terminate) the process?
Here is what I have tried:
DataModel class:
class DataModel : ObservableObject {
@Published var task = Process()
func RunTask() {
// add arguments to task here
do {
task.arguments = [args]
try task.run()
} catch {}
}
Here is the ViewTask() class:
struct ViewTask: View {
@ObservedObject var datamodel: DataModel
var body: some View {
///if user presses a cancel button, cancel the task running
datamodel.task.terminate()
}
This doesn't work because the task can't find the arguments. But when I don't make task an @Published variable, and it only exists locally in that Do loop, it works fine.
Maybe I need to declare/initialize it differently in the datamodel?
Thanks