单军华
2018-05-04 25f409185a53e5e7beb17518a684298d92d31b3f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
//
//  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