我有一个后台任务要从后台运行的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];
});
});
这是我在表格单元格的选择上导航的方式:
-(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的任何帮助?
提前谢谢。
不能挂起/恢复全局并发队列。 此外,您发布的代码似乎同步执行。如果不将其修改为"可取消"(即 -getNewsStartingFrom:toEndLimit:
将需要在某处检查一个指示取消的变量并自愿停止自己。我不知道你是否拥有NewsResponseBO
,但就在我的头顶上,这听起来像是NSURLConnection
的好工作,并且由于NSOperationQueue/NSOperation
支持取消而同时NSOperation
(但请注意,NSOperation
仍然不会让你硬取消)。
有关取消待处理调度作业以及为什么无法硬取消的更多详细信息,请参阅我对此问题的回答。