如何在GCD后台运行的全局队列中终止或暂停作业

时间:2013-10-21 07:47:42

标签: multithreading ios5 ios6 grand-central-dispatch dispatch

我有一个后台任务可以从后台运行的webservice下载,如果用户同时导航到其他屏幕,我想暂停它。

以下是我尝试在后台下载的方式:

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NewsResponseBO *objNewsRspnc = [objNewsParser getNewsStartingFrom:0 toEndLimit:10];
dispatch_async(dispatch_get_main_queue(), ^{
for (int i=0; i< [objNewsRspnc.arrNewsData count]; i++) {
[arrListOfNews addObject:[objNewsRspnc.arrNewsData objectAtIndex:i]];
}
isDataLoading = NO;
isBottomLoaderAdded = NO;
[loader stopAnimating];
[loader removeFromSuperview];
[bottomViewforLoader removeFromSuperview];
tbv_ListOfNews.frame = CGRectMake(tbv_ListOfNews.frame.origin.x,   tbv_ListOfNews.frame.origin.y, tbv_ListOfNews.frame.size.width, tbv_ListOfNews.frame.size.height +80);
tbv_ListOfNews.contentOffset = CGPointMake(0, tbv_ListOfNews.contentSize.height);
[tbv_ListOfNews reloadData];
    });
});

以下是我如何浏览tablecell的选择:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
if (bottomViewforLoader != nil) {
tbv_ListOfNews.frame = CGRectMake(0, 44, 320, 416+APP_DELEGATE.floatExtraHeight) ;
[bottomViewforLoader removeFromSuperview];
}
[tableView deselectRowAtIndexPath:indexPath animated:NO];
NewsDetailViewController *obj_DetailNews = [[NewsDetailViewController alloc]initWithNibName:nil bundle:[NSBundle mainBundle]];
NSLog(@"indexPath.row:%d,arrListOfNews(number of object:%d)  ",indexPath.row,[arrListOfNews count]);
obj_DetailNews.obj_newsDetail = [arrListOfNews objectAtIndex:indexPath.row];
[APP_DELEGATE.navController pushViewController:obj_DetailNews animated:YES];
}

有关如何暂停dispatch_get_global_queue的任何帮助?

先谢谢。

1 个答案:

答案 0 :(得分:0)

您无法暂停/恢复全局并发队列。此外,您发布的代码似乎同步执行。如果不将其修改为“可取消”,您将无法取消它(即-getNewsStartingFrom:toEndLimit:将需要在某处检查指示取消的变量并自愿停止。)我不知道您是否拥有{{1或者不是,但仅仅是我的头脑,这听起来像NewsResponseBONSURLConnection一样好,因为NSOperation支持取消(但请注意NSOperationQueue/NSOperation仍然没有让你取消硬盘。

请参阅我对this question的回答,了解有关取消待处理调度作业的详细信息以及无法进行硬取消的原因。