If you want to control one task to execute at the end,you can use dispatch_group_t,If you want one task not only to execute after some tasks but also to execute before some tasks,you can use dispatch_barrier_sync:
dispatch_queue_t queue = dispatch_queue_create("com.example.gcd", DISPATCH_QUEUE_CONCURRENT);
dispatch_async(queue, ^{ printf("1");});
dispatch_async(queue, ^{ printf("2");});
dispatch_barrier_sync(queue, ^{ printf("3");});
dispatch_async(queue, ^{ printf("4");});
dispatch_async(queue, ^{ printf("5");});
it may print
12345 or 21354 or ... but 3 always after 1 and 2, and 3 always before 4 and 5
If your want to control the order exactly, you can use dispatch_sync or Serial Dispatch Queue,or NSOperationQueue.If you use NSOperationQueue,use the method of "addDependency" to control the order of tasks:
NSOperationQueue *queue = [[NSOperationQueue alloc] init];
NSBlockOperation *op1 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"op 1");
}];
NSBlockOperation *op2 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"op 2");
}];
NSBlockOperation *op3 = [NSBlockOperation blockOperationWithBlock:^{
NSLog(@"op 3");
}];
//op3 is executed last after op2,op2 after op1
[op2 addDependency:op1];
[op3 addDependency:op2];
[queue addOperation:op1];
[queue addOperation:op2];
[[NSOperationQueue mainQueue] addOperation:op3];
It will always print:
op1 op2 op3