// // GCDPage.m // BaseProject // // Created by WindShan on 2016/12/9. // Copyright © 2016年 WindShan. All rights reserved. // #import "GCDPage.h" @interface GCDPage () @end @implementation GCDPage - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ //子线程异步执行下载任务,防止主线程卡顿 NSURL *url = [NSURL URLWithString:@"http://www.baidu.com"]; NSError *error; NSString *htmlData = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:&error]; if (htmlData != nil) { //异步返回主线程,根据获取的数据,更新UI dispatch_async(dispatch_get_main_queue(), ^{ NSLog(@"根据更新UI界面"); }); } else { NSLog(@"error when download:%@",error); } }); } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } /* #pragma mark - Navigation // In a storyboard-based application, you will often want to do a little preparation before navigation - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller. } */ @end