Junior — Middle
What tools or approaches do you use to cancel or postpone tasks in your work?
sobes.tech AI
Answer from AI
To cancel or postpone tasks in iOS development, the following approaches and tools are often used:
- OperationQueue and Operation — allow managing task execution, pausing or canceling operations.
- DispatchWorkItem with GCD — can cancel or delay the execution of a code block.
- Timers (Timer) — for delayed code execution.
- Flags and states — within the code, you can check whether to execute a task or if it has been canceled.
Example of using DispatchWorkItem to cancel a task:
var workItem: DispatchWorkItem?
func startTask() {
workItem = DispatchWorkItem {
print("Executing task")
}
DispatchQueue.global().asyncAfter(deadline: .now() + 5, execute: workItem!)
}
func cancelTask() {
workItem?.cancel()
print("Task canceled")
}
Thus, you can flexibly manage task execution in your app.