In SwiftUI, the new task modifier handles creating a new task on a UI action or on view appearance. It also ensures that a task isn't restarted on a second action if the task from the first action is still in progress.
What is the best way of getting the same behavior in non-SwiftUI code? Here's my best attempt:
class Controller: UIViewController {
var task: Task? = nil
// ...
func didTapStart() {
guard self.task == nil || self.task.isCancelled else { return }
self.task = Task { .... }
}
}
Will this work? Will isCancelled be true when a task successfully completes, or only if it's explicitly cancelled? Is there a better way?