dispatch_async, dispatch_sync different

1.
-(void)GetdataAndReloadData:(int) CurrentPageIndex
{
dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_async(queue, ^(void){      //如果用dispatch_sync的话不能看到点击后cell.textlabel.text变成(正在加载。。。。)
[JsonParse GetModelList:CurrentPageIndex OnFinsh:^(NSMutableArray *dic)
{
SubjectModelList=dic;
dic=nil;
dispatch_queue_t main=dispatch_get_main_queue();
dispatch_async(main, ^(void){
[_SubjectList reloadData];
[_SubjectList scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:1 inSection:0]  atScrollPosition:UITableViewScrollPositionTop animated:NO];
});
}];

});

}

//delegate
– (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (indexPath.row==0)
{
if(PageIndex==1)//第一页无法加载上一页
return;
UITableViewCell *cell=[_SubjectList cellForRowAtIndexPath:indexPath];
cell.textLabel.text=@”正在加载…”;
[self GetdataAndReloadData:–PageIndex];

return;
}
if(indexPath.row==SubjectModelList.count+1)
{
UITableViewCell *cell=[_SubjectList cellForRowAtIndexPath:indexPath];
cell.textLabel.text=@”正在加载…”;
[self GetdataAndReloadData:++PageIndex];

return;
}
ContentViewController *content=[[ContentViewController alloc] init:[SubjectModelList objectAtIndex:indexPath.row-1] ];
[self.navigationController pushViewController:content animated:YES];
content=nil;

}

1.用dispatch_async 输出结果

2012-08-14 18:21:00.928 NcuhomeBBS[1620:10103] 方法return          <———————–}
2012-08-14 18:21:00.930 NcuhomeBBS[1620:14d27] 队列开始工作                                                         }
2012-08-14 18:21:01.021 NcuhomeBBS[1620:14d27] 队列获取到数据了                        80多毫秒
2012-08-14 18:21:01.022 NcuhomeBBS[1620:10103] 界面开始更新      <————————-}

2.用dispatch_sync 输出结果

2012-08-14 18:15:32.388 NcuhomeBBS[1580:10103] 队列开始工作
2012-08-14 18:15:32.458 NcuhomeBBS[1580:10103] 队列获取到数据了
2012-08-14 18:15:32.458 NcuhomeBBS[1580:10103] 方法return           <————————} 同时
2012-08-14 18:15:32.458 NcuhomeBBS[1580:10103] 界面开始更新       <————————}

从中可以看出不是dispatch_sync比dispatch_async快 而是dispatch_sync比dispatch_sync更早 方法return

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s