0

I have a background task to download from a webservice which runs in background and i want to suspend it if user navigates to other screens meanwhile.

Here is how i tried to download in background:

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];
    });
});

and Here is how i navigate on tablecell's selection:

-(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];
}

any help on how to suspend dispatch_get_global_queue ?

Thanks in Advance.

Neeraj
  • 239
  • 5
  • 14

1 Answers1

0

You cannot suspend/resume the global concurrent queues. Furthermore, the code you've posted appears to execute synchronously. You will not be able to cancel it without modifying it to be be "cancelable" (i.e. -getNewsStartingFrom:toEndLimit: will need to check a variable somewhere indicating cancellation and voluntarily stop itself.) I don't know if you own NewsResponseBO or not, but just off the top of my head, this sound like a good job for NSURLConnection and a concurrent NSOperation owing to NSOperationQueue/NSOperation's support for cancellation (but note that NSOperations still don't get you hard cancellation).

See my answer to this question for more detail on cancellation of pending dispatch jobs and why hard cancellation isn't possible.

Community
  • 1
  • 1
ipmcc
  • 29,581
  • 5
  • 84
  • 147