<commit_before>## 问题,刷新表视图没有反应.


思路:

- 下载数据
- 下载完成
- 解析数据
- 数据===>数据模型===>数组
- 刷表

代码片段

[ZXNewsViewController.m#L122,#L155](https://github.com/urmyfaith/roadofios/blob/master/advancedUI/0112_week11_day3/0112_project_ipaddown/ZXNews/ZXNews/ZXNewsViewController.m#L122,#L155)

~~~objectivec
   
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer  = [AFHTTPResponseSerializer serializer];
    [manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%s [LINE=%i] 下载数据完成",__func__,__LINE__);
        [self analyJSON:responseObject];//解析数据
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%s [LINE=%i] 下载数据失败",__func__,__LINE__);
    }];
    
    //结束刷新
    [self.refreshHeaderView endRefreshing];
    [self.refreshFooterView endRefreshing];
    
}

-(void)analyJSON:(NSData *)data{
    
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    if ([self.datas count] > 0) {
        [self.datas removeAllObjects];
    }
    for (NSDictionary *subDic in array) {
        ZXNewsModel *model = [ZXNewsModel modelWithDic:subDic];
        
        [self.datas addObject:model];
    }
   [self.tableView reloadData]; 
}
~~~

## 分析

在block里操作了UI,(即为在异步线程中操作主线程/UI线程)

## 解决方法

将刷UI放到主线程中.

~~~objectivec
    for (NSDictionary *subDic in array) {
        ZXNewsModel *model = [ZXNewsModel modelWithDic:subDic];
        
        [self.datas addObject:model];
    }
    [self performSelectorOnMainThread:@selector(myReloadTable) withObject:nil waitUntilDone:NO];
    
}

-(void)myReloadTable{
    [self.tableView reloadData];
    NSLog(@"%s [LINE=%i] 刷表完成 ",__func__,__LINE__);
}
~~~

## 参考资料(重要)


- 1.)http://stackoverflow.com/questions/25577341/reload-tableview-with-addoperationwithblock
- 2.)http://stackoverflow.com/questions/19812058/uitableview-data-reload-issue
- 3.)http://likid1412.github.io/blog/2013/10/28/ios-stanford-university-ios-shi-pin-bi-ji/


<commit_msg>使用GCD修改<commit_after>## 问题,刷新表视图没有反应.


思路:

- 下载数据
- 下载完成
- 解析数据
- 数据===>数据模型===>数组
- 刷表

代码片段

[ZXNewsViewController.m#L122,#L155](https://github.com/urmyfaith/roadofios/blob/master/advancedUI/0112_week11_day3/0112_project_ipaddown/ZXNews/ZXNews/ZXNewsViewController.m#L122,#L155)

~~~objectivec
   
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer  = [AFHTTPResponseSerializer serializer];
    [manager GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"%s [LINE=%i] 下载数据完成",__func__,__LINE__);
        [self analyJSON:responseObject];//解析数据
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"%s [LINE=%i] 下载数据失败",__func__,__LINE__);
    }];
    
    //结束刷新
    [self.refreshHeaderView endRefreshing];
    [self.refreshFooterView endRefreshing];
    
}

-(void)analyJSON:(NSData *)data{
    
    NSArray *array = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    if ([self.datas count] > 0) {
        [self.datas removeAllObjects];
    }
    for (NSDictionary *subDic in array) {
        ZXNewsModel *model = [ZXNewsModel modelWithDic:subDic];
        
        [self.datas addObject:model];
    }
   [self.tableView reloadData]; 
}
~~~

## 分析

在block里操作了UI,(即为在异步线程中操作主线程/UI线程)

## 解决方法

将刷UI放到主线程中.

~~~objectivec
    for (NSDictionary *subDic in array) {
        ZXNewsModel *model = [ZXNewsModel modelWithDic:subDic];
        
        [self.datas addObject:model];
    }
    [self performSelectorOnMainThread:@selector(myReloadTable) withObject:nil waitUntilDone:NO];
    
}

-(void)myReloadTable{
    [self.tableView reloadData];
    NSLog(@"%s [LINE=%i] 刷表完成 ",__func__,__LINE__);
}
~~~

使用GCD的方法:

a)方法一

~~~objectivec
	static dispatch_once_t onceToken;
	dispatch_once(&onceToken, ^{
			[_tableView reloadData];
	});
~~~

b)方法二

~~~objectivec

     dispatch_queue_t queue= dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0) ; 
     
     dispatch_async(queue, ^{
          dispatch_async(dispatch_get_main_queue(),^{
               [self.tableView reloadData]; 
               // reloadData will load your table with contents
          });
~~~

## 参考资料(重要)


- 1.)http://stackoverflow.com/questions/25577341/reload-tableview-with-addoperationwithblock
- 2.)http://stackoverflow.com/questions/19812058/uitableview-data-reload-issue
- 3.)http://likid1412.github.io/blog/2013/10/28/ios-stanford-university-ios-shi-pin-bi-ji/



{'fraction_non_alphanumeric': 0.13645182151325716, 'fraction_numerical': 0.023927570597111446, 'mean_word_length': 3.9152542372881354, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1068584', 'n_tokens_mistral': 1872, 'n_tokens_neox': 1779, 'n_words': 226}