Skip to content

Instantly share code, notes, and snippets.

@sayler8182
Created November 17, 2020 12:05
Show Gist options
  • Save sayler8182/77a5a2b7c2e53b1b46c654821be7013e to your computer and use it in GitHub Desktop.
Save sayler8182/77a5a2b7c2e53b1b46c654821be7013e to your computer and use it in GitHub Desktop.
var item: DispatchWorkItem!
item = DispatchWorkItem {
sleep(2)
guard !item.isCancelled else {
print("Work has been cancelled")
return
}
print("Work has been done")
}
DispatchQueue.global().async(execute: item)
DispatchQueue.global().async {
sleep(1)
item.cancel()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment