0

I have this code:

_myQueue = dispatch_queue_create("com.myapp", DISPATCH_QUEUE_SERIAL);
            _mainQueue = dispatch_get_main_queue();

and lot of this block that require some seconds (or minutes)

dispatch_async(_myQueue,
               ^{
                  if(canRun){ 
                    dispatch_async(_mainQueue,^{/* updating interface here */});
                    // code here
                  }
                });

My app have a "Stop" button to try stopping all job, and the BOOL "canRun" help me to execute all blocks w/o do nothing.....but always I have to wait the completition of each block until the queue come 0. Is there any way to instantly "clean" the queue istead doing that? The aim is to stop processes and to start over without closing and reopening the application. This project works under ARC.

Mike97
  • 596
  • 5
  • 20
  • 2
    Why not use NSOperationQueue? You're just reinventing an excellent wheel that you've already been given... – matt Dec 25 '14 at 16:20
  • 1
    Perhaps this post could be useful? http://stackoverflow.com/questions/9546385/kill-items-in-a-dispatch-async-queue-in-ios – gimpycpu Dec 25 '14 at 16:21
  • Yep, I found it easy to update the interface in this way... – Mike97 Dec 25 '14 at 16:22
  • Guys I admit that I have never used it, but reading here:http://stackoverflow.com/questions/12660706/nsoperation-cancelalloperations-does-not-stop-the-operation, they seem to have the same problem, that everything has to be completed. I'm wrong? thank you – Mike97 Dec 25 '14 at 16:37
  • What I mean is that I need a bool in each block, w/o reinventing the well I'm writing less line of code.. – Mike97 Dec 25 '14 at 16:44

1 Answers1

2

You can cancel them if you take a few extra steps to creat a dispatch_source object and keep a reference to it.

Review this for starters

https://developer.apple.com/library/mac/documentation/Performance/Reference/GCD_libdispatch_Ref/index.html

There are functions to pause, resume and cancel.

uchuugaka
  • 12,679
  • 6
  • 37
  • 55