iOS GCD(9)

来源:未知 责任编辑:责任编辑 发表时间:2014-01-26 21:59 点击:

    NSArray *array = [[NSArray alloc] initWithObjects:@"string1",@"string2",@"string3", nil]; 
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
    dispatch_apply([array count], queue, ^(size_t index){ 
        NSLog(@"%zu : %@",index,[array objectAtIndex:index]); 
    }); 

//假如对一个NSArray类对象的所有元素执行处理时,不必一个个编写for循环
    NSArray *array = [[NSArray alloc] initWithObjects:@"string1",@"string2",@"string3", nil];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_apply([array count], queue, ^(size_t index){
        NSLog(@"%zu : %@",index,[array objectAtIndex:index]);
    });


由于dispatch_apply方法也与dispatch_sync方法相同,会等待处理执行结束,因此推荐在dispatch_async函数中非同步的执行dispatch_apply方法。

例如:


[cpp]
NSArray *array = [[NSArray alloc] initWithObjects:@"string1",@"string2",@"string3", nil]; 
   dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
   //在Glocbal Dispatch Queue中非同步执行  
   dispatch_async(queue, ^{ 
        
       //等待dispatch_apply方法中的全部处理执行结束  
       dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); 
       dispatch_apply([array count], queue, ^(size_t index){ 
           NSLog(@"%zu : %@",index,[array objectAtIndex:index]); 
       }); 
        
       //dispatch_apply方法中的全部处理任务执行结束  
        
       dispatch_async(dispatch_get_main_queue(), ^{ 
          //在主线程中执行处理  
           NSLog(@"done"); 
       }); 
        
   }); 

 NSArray *array = [[NSArray alloc] initWithObjects:@"string1",@"string2",@"string3", nil];
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);

发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
用户名: 验证码:点击我更换图片
最新评论 更多>>

推荐热点

  • Lexical or Preprocessor Issue 'xxx.h
  • ios学习笔记(二)xcode 4.3.2下实现基本交互
  • ios版本的helloworld
  • iphone(object-c) 内存管理(3) 有效的内存管理 前半部分
  • ios学习笔记(一)xcode 4.3.2下创建第一个ios项目
  • UITableView一些方法
  • IOS类似iphone通讯录TableView的完整demo【附源码】
  • [iPhone中级]iPhone团购信息客户端的开发 (二)
  • iphone(object-c)内存管理(1)
网站首页 - 友情链接 - 网站地图 - TAG标签 - RSS订阅 - 内容搜索
Copyright © 2008-2015 计算机技术学习交流网. 版权所有

豫ICP备11007008号-1