GCD in iOS

GCD (Grand Central Dispatch) is one very powerful way of multi-thread to handle many tasks at the same time. I read many articles which say that GCD is much better than other ways, such as NSThread, NSOperationQueue, and NSInvocationOperation, but I'm not sure about this. Maybe GCD is mainly focus on the CPU which has multi core after iOS 4.0 and beyond. The following are some code I wrote to get familiar with GCD.


- (void)viewDidLoad

{

    [super viewDidLoad];


    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem allocinitWithImage:[UIImageimageNamed:@"AddStack.png"style:UIBarButtonSystemItemAdd target:selfaction:@selector(performMethod)];

    

    self.indicator = [[UIActivityIndicatorView allocinitWithFrame:CGRectMake(16020020,20)];

    self.indicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyleGray;

    self.indicator.hidesWhenStopped = YES;

    

    [self.view addSubview:self.indicator];

    [self.indicator release];

    

}


- (void)performMethod {

    

    self.indicator.hidden = NO;

    [self.indicator startAnimating];

    

    //Open another new async operation in the global queue

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT0), ^{


        NSURL * url = [NSURL URLWithString:@"http://www.apple.com"];

        NSError * error;

        NSString * data = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncodingerror:&error];

        

        if (data != nil) {


    //Inform main thread to refresh UI.

            dispatch_async(dispatch_get_main_queue(), ^{

                [self.indicator stopAnimating];

                self.indicator.hidden = YES;

                

                NSLog(@"Result: %@", data);

            });

            

        } else {

            

            dispatch_async(dispatch_get_main_queue(), ^{

                [self.indicator stopAnimating];

                self.indicator.hidden = YES;

                

                NSLog(@"error when download:%@", error);

            });

            

        }

        

    });

}



Another similiar example is as below.

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath*)indexPath

{

    static NSString *cellIdentity = @"ImageViewCell";

    

    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentity];

    

    if (cell ==  nil) {

        cell = [[[UITableViewCell allocinitWithStyle:UITableViewCellStyleDefaultreuseIdentifier:cellIdentity] autorelease];

    }

    

    UIImageView *imageView = [[UIImageView allocinitWithFrame:CGRectMake(559090)];

    [cell.contentView addSubview:imageView];

    [imageView release];

    

    dispatch_async(dispatch_get_global_queue(00), ^{


        UIImage *img = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[imagesUrls objectAtIndex:indexPath.row]]]];


        dispatch_async(dispatch_get_main_queue(), ^{


            [imageView setImage:img];

        });

    });

    

    return cell;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值