1

I have 2 methods:

-(void)clear
{
  // some code

  dispatch_async( dispatch_get_global_queue(0,0), 
        ^{
              // async_block_1

         });     
}

-(void)download
{
  // some code

  dispatch_async( dispatch_get_global_queue(0,0), 
        ^{
              // async_block_2

         });     
}

And I need call'em in 3rd method:

-(void)relosd
{
   [self clear];
   [self download];
}

How I can guaranted perform first async_block_1 then async_block_2?

Obvious that the following code does not guarantee this:

-(void)reload
{
    dispatch_sync( dispatch_get_global_queue(0,0), 
       ^{
           [self clear];
           [self download];
       });
}
2
  • Could you do this as a "NSOperationQueue" thing with "setMaxConcurrentOperationCount:" set to 1? Commented May 19, 2014 at 14:17
  • Michael, yes, I can. But why? CGD also can do it with serial queue. But I need parallel working, that will sinchronized sometimes. Commented Jun 9, 2014 at 14:24

2 Answers 2

3

GCD queue can be create to run as a serial queue.

Just create your own queue:

dispatch_queue_t queue = dispatch_queue_create("com.my.download", NULL);

You can do this .m part where you functie are declared something like:

dispatch_queue_t getDownloadQueue() {
    static dispatch_once_t onceToken;
    static dispatch_queue_t ruleQueue;
    dispatch_once(&onceToken, ^{
        ruleQueue = dispatch_queue_create("com.my.download", NULL);
    });

    return ruleQueue;
}

Just place this code outside of your @implementation, after which you can just do something like:

-(void)clear {
  // some code
  dispatch_async( getDownloadQueue(), ^{
    // async_block_1
  });     
}

-(void)download {
  // some code

  dispatch_async(getDownloadQueue(), ^{
    // async_block_2
  });     
}

If you only need the queue in one instance of you class then use a property, this example will create a queue which you can use throughout your app and makes sure that they are executed in order.

Sign up to request clarification or add additional context in comments.

Comments

2

You are probably after what rckoenes is suggesting but your other options are.

  • Use NSOperationQueue and set dependencies
  • Use NSOperationQueue and set maxConcurrentOperationCount
  • Use dispatch_barrier_async instead of dispatch_async

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.