Chinaunix首页 | 论坛 | 博客
  • 博客访问: 713237
  • 博文数量: 102
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 1748
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-23 15:42
个人简介

寻找严肃、沉默和专注的力量。

文章分类

全部博文(102)

文章存档

2015年(26)

2014年(8)

2013年(68)

分类: iOS平台

2015-06-05 14:39:28


点击(此处)折叠或打开

  1. dispatch_queue_t dispatchQueue = dispatch_queue_create("ted.queue.next", DISPATCH_QUEUE_CONCURRENT);
  2.     dispatch_group_t dispatchGroup = dispatch_group_create();
  3.     dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
  4.         NSLog(@"dispatch-1");
  5.     });
  6.     dispatch_group_async(dispatchGroup, dispatchQueue, ^(){
  7.         NSLog(@"dspatch-2");
  8.     });
  9.     dispatch_group_notify(dispatchGroup, dispatch_get_main_queue(), ^(){
  10.         NSLog(@"end");
  11.     });



手动管理

点击(此处)折叠或打开

  1. dispatch_group_enter(group);

  2. dispatch_async(queue, ^{

  3.   //。。。

  4.   dispatch_group_leave(group);

  5. });

例子:

点击(此处)折叠或打开

  1. dispatch_group_t group = dispatch_group_create();

  2. MyCoreDataObject *coreDataObject;

  3. dispatch_group_enter(group);
  4. AFHTTPRequestOperation *operation1 = [[AFHTTPRequestOperation alloc] initWithRequest:request1];
  5. [operation1 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  6.     coreDataObject.attribute1 = responseObject;
  7.     sleep(5);
  8.     dispatch_group_leave(group);
  9. }];
  10. [operation1 start];

  11. dispatch_group_enter(group);
  12. AFHTTPRequestOperation *operation2 = [[AFHTTPRequestOperation alloc] initWithRequest:request1];
  13. [operation2 setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
  14.     coreDataObject.attribute2 = responseObject;
  15.     sleep(10);
  16.     dispatch_group_leave(group);
  17. }];
  18. [operation2 start];

  19. dispatch_group_wait(group, DISPATCH_TIME_FOREVER);
  20. dispatch_release(group);

  21. [context save:nil];

dispatch_barrier_async


点击(此处)折叠或打开

  1. dispatch_queue_t concurrentQueue = dispatch_queue_create("my.concurrent.queue", DISPATCH_QUEUE_CONCURRENT);
  2.     dispatch_async(concurrentQueue, ^(){
  3.         NSLog(@"dispatch-1");
  4.     });
  5.     dispatch_async(concurrentQueue, ^(){
  6.         NSLog(@"dispatch-2");
  7.     });
  8.     dispatch_barrier_async(concurrentQueue, ^(){
  9.         NSLog(@"dispatch-barrier");
  10.     });
  11.     dispatch_async(concurrentQueue, ^(){
  12.         NSLog(@"dispatch-3");
  13.     });
  14.     dispatch_async(concurrentQueue, ^(){
  15.         NSLog(@"dispatch-4");
  16.     })




阅读(1084) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~