If you want to control one task for execution at the end, you can use dispatch_group_t. If you want a single task to be performed not only after completing some tasks, but also to perform 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");});
he can print
12345 or 21354 or ... but 3 always after 1 and 2, and 3 always before 4 and 5
If you want precise control over the order, you can use dispatch_sync or Serial Dispatch Queue or NSOperationQueue. If you are using NSOperationQueue, use the addDependency method 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"); }];
He will always print:
op1 op2 op3
ElonChan Feb 28 '15 at 14:00 2015-02-28 14:00
source share