zoukankan      html  css  js  c++  java
  • objective-c GCD

      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 <UIKit/UIKit.h>
    2 
    3 @interface MainViewController : UIViewController<UITableViewDelegate,UITableViewDataSource>
    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 *)nibBundleOrNil
    10 {
    11     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    12     if (self) {
    13         // Custom initialization
    14     }
    15     return self;
    16 }
    17 
    18 - (void)viewDidLoad
    19 {
    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)didReceiveMemoryWarning
    33 {
    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

  • 相关阅读:
    Android防止按钮连续点击
    Android中的AlertDialog遇到的错误
    android通过Jni加载so库遇到UnsatisfiedLinkError问题!!!
    接口回调
    Android中的APinner2
    AndroidAPI
    Android中的下拉列表
    学习地址
    2018/12/21:Date类
    2018/12.21:找出数组最大项和最小项。
  • 原文地址:https://www.cnblogs.com/mo-shou/p/3514521.html
Copyright © 2011-2022 走看看