2

I created a dispatch_async to download data to a web services.

I created an alert with a button from the possibilida to cancel / block the download.

My question is:

How can I delete / stop my dispatch_async?

This is my dispatch:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
    doStuff();
}
vikingosegundo
  • 52,040
  • 14
  • 137
  • 178
Stephany
  • 994
  • 1
  • 7
  • 18

1 Answers1

6

There's no direct way to do that with GCD, but you could use a NSOperationQueue and call the method cancelAllOperations. NSOperationQueue uses GCD internally anyway.

Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Micky
  • 5,578
  • 7
  • 31
  • 55
  • I was reading this in this post: http://stackoverflow.com/questions/9546385/kill-items-in-a-dispatch-async-queue-in-ios But I did not understand, you can give an example? Thank You – Stephany Feb 17 '15 at 10:10
  • It's a bit more overhead than just that dispatch call right now. You can check this tutorial for starters: http://www.raywenderlich.com/19788/how-to-use-nsoperations-and-nsoperationqueues – Micky Feb 17 '15 at 10:14
  • If this answer helped you, consider accepting it so others know which solution worked for you. – Micky Feb 18 '15 at 09:31