2013-10-21 81 views
0

我有一個後臺任務可以從後臺運行的web服務下載,如果用戶同時導航到其他屏幕,我想暫停它。如何殺死或暫停在GCD後臺運行的全局隊列上的作業

這裏是我試圖在後臺下載:

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任何幫助嗎?

在此先感謝。

回答

0

您不能暫停/恢復全局併發隊列。此外,您發佈的代碼似乎是同步執行的。你將無法取消它,而不修改它是「可取消」(即-getNewsStartingFrom:toEndLimit:將需要檢查某個地方的變量,表明取消並自動停止)。我不知道你是否擁有NewsResponseBO,但只是關閉我的頭頂,這聽起來好像NSURLConnectionNSOperation併發NSOperationQueue/NSOperation支持取消(但請注意,NSOperation仍然不會讓你難以取消)。

查看我對this question的回答,以獲取更多關於取消待處理派遣工作的信息以及爲何無法取消強制取消。

相關問題