博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
objective-c GCD
阅读量:4622 次
发布时间:2019-06-09

本文共 3007 字,大约阅读时间需要 10 分钟。

  GCD应该是iOS开发中最常见的多线程使用方法.

 

1 dispatch_sync(queue, block)//同步提交

 

1 dispatch_async (queue, block)// 异步提交

 

1 dispatch_after(time, queue, block)// 同步延迟提交

 

 

其中第一个参数类型是dispatch_queue_t,就是一个表示队列的数据结构 typedef struct dispatch_queue_s *dispatch_queue_t;

block就是表示任务的Block typedef void (^dispatch_block_t)( void);

 

dispatch_async函数是异步非阻塞的,调用后会立刻返回,工作由系统在线程池中分配线程去执行工作。 dispatch_sync和dispatch_after是阻塞式的,会一直等到添加的工作完成后才会返回。

 

ViewController.h

1 #import 
2 3 @interface MainViewController : UIViewController
4 5 6 @property(nonatomic,retain)UITableView *table;7 @property(nonatomic,copy)NSArray *arr;8 9 @end

ViewController.m

1 #import "MainViewController.h" 2  3 @interface MainViewController () 4  5 @end 6  7 @implementation MainViewController 8  9 - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil10 {11     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];12     if (self) {13         // Custom initialization14     }15     return self;16 }17 18 - (void)viewDidLoad19 {20     [super viewDidLoad];21     // Do any additional setup after loading the view.22     NSURL *url1 = [NSURL URLWithString:@"http://img4.duitang.com/uploads/item/201302/07/20130207172119_HJP8F.jpeg"];23     NSURL *url2 = [NSURL URLWithString:@"http://img1.gamersky.com/image2013/01/20130111y_4/image003_wm.jpg"];24     _arr = [NSArray arrayWithObjects:url1,url2, nil];25     26     _table = [[UITableView alloc]initWithFrame:CGRectMake(0, 20, 320, 400)];27     _table.delegate = self;28     _table.dataSource = self;29     [self.view addSubview:_table];30 }31 32 - (void)didReceiveMemoryWarning33 {34     [super didReceiveMemoryWarning];35     // Dispose of any resources that can be recreated.36 }37 38 39 -(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{40     return 1;41 }42 43 -(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{44     return [_arr count];45 }46 47 -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{48     static NSString *identifier = @"identifier";49     UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];50     if (cell == nil) {51         cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];52     }53     54     // 开始执行队列,第1个参数表示将任务交给谁来处理,第2个为函数体,表示你要做的事情55     //将一些耗时的工作添加到全局队列,让系统分配线程去做,56     dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{57         58         NSData *data = [NSData dataWithContentsOfURL:[_arr objectAtIndex:[indexPath row]]];59         UIImage *img = [UIImage imageWithData:data];60         //工作完成后再次调用GCD的主线程队列去完成UI相关的工作,这样做就不会因为大量的非UI相关工作加重主线程负担,从而加快UI事件响应。61             dispatch_async(dispatch_get_main_queue(), ^{62                 cell.imageView.image = img;63             });64         65     });66     return cell;67 }68 @end

 

 

 

继续挖个坑。。。准备重写T.T

转载于:https://www.cnblogs.com/mo-shou/p/3514521.html

你可能感兴趣的文章
【Hive学习之六】Hive Lateral View &视图&索引
查看>>
【搜索】POJ-3050 基础DFS
查看>>
3.Arm机器码
查看>>
PHP防止Xss攻击
查看>>
Red and Black(DFS深搜实现)
查看>>
linux清除文件内容
查看>>
python 学习笔记 多进程
查看>>
刚 安装 Oracle时,登录会出现的问题, ora-28000: the account is locked
查看>>
redis
查看>>
Lua与C++ 第三篇(简单解析Lua的堆栈)
查看>>
[置顶] HMM Tutorial 隐马尔科夫模型
查看>>
Android二维码功能实现,在程序内嵌入ZXing项目
查看>>
关于完美的练习
查看>>
Keras实现autoencoder
查看>>
跟我学SharePoint 2013视频培训课程——网站导航及页面元素(2)
查看>>
任务调度quartz
查看>>
低秩分解
查看>>
python3--正则表达式
查看>>
[原创] 算法之递归(3)- 链表操作
查看>>
1221结构体与数组结合应用实例
查看>>