0

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

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

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 的任何帮助?

提前致谢。

4

1 回答 1

0

您不能暂停/恢复全局并发队列。此外,您发布的代码似乎是同步执行的。如果不将其修改为“可取消”,您将无法取消它(即-getNewsStartingFrom:toEndLimit:需要在某处检查指示取消的变量并自愿停止。)我不知道您是否拥有NewsResponseBO,但就在顶部在我看来,这听起来不错,NSURLConnection而且NSOperation由于NSOperationQueue/NSOperation' 支持取消(但请注意NSOperation s 仍然不会让你硬取消),这听起来不错。

请参阅我对这个问题的回答,了解有关取消待处理调度作业以及为什么无法进行硬取消的更多详细信息。

于 2013-10-21T12:01:16.560 回答