3

I want to know the way for stopping asyncronous task when i click back button in navigation bar.I have done this code,but its not working ... dispatch_group_t imageQueue = dispatch_group_create();

dispatch_group_async(imageQueue, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0),
                     ^{
                         imagedata=[[NSMutableArray alloc]init];

                         for (int i=0; i<[imageURL count]; i++)
                         {
                             NSLog(@"clicked ");

                           [imagedata addObject:[UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:[imageURL objectAtIndex:i]]]]];

 [[NSNotificationCenter defaultCenter] postNotificationName:@"Avatardownloaded" object:imagedata   userInfo:nil];
                             }




                     });

in viewdisappear....

-(void)viewDidDisappear:(BOOL)animated
{
    dispatch_suspend(imageQueue);

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"Avatardownloaded" object:nil];
    [[NSNotificationCenter defaultCenter]release];
    [avatarimages release];
    [imagedata release];
    [imageURL release];   
}

even though i suspend the thread it doesnt stop execution its keepon running in background.Anyone pls help me

4 Answers4

4

Grand Central Dispatch is suitable for fire-and-forget tasks. If you want cancellable tasks, I would recommend using NSOperation and maybe NSOperationQueue.

Michał Ciuba
  • 7,876
  • 2
  • 33
  • 59
  • 1
    One more doubt..i am using SBJson Parser,bcz of that i couldnt parse newline,carriage return characters..For parsing i removed those characters,,but in UI how to show statements with newlines which i parsed using SBJson.. –  Sep 12 '14 at 12:21
1

No, you cannot do this. Use NSTimer instead.

Mariano
  • 330
  • 1
  • 4
  • 13
1

dispatch_queue does not support cancel. However, you can use a block variable or some global variable to keep track of cancelable in the code of your dispatch_queue. The code in your dispatch_queue have to be able to stop working while cancel = YES (for example).

Duyen-Hoa
  • 15,384
  • 5
  • 35
  • 44
0

There are facilities such as dispatch_suspend() (along with dispatch_resume()). This will prevent any new blocks on a particular queue from being scheduled. It will not have any impact on already-running blocks. If you want to pause a block already scheduled and running, it is up to your code to check some conditions and pause. For suspending queues/cancelling operations, you should use NSOperation and NSOperationQueue instead of GCD.

NSPratik
  • 4,714
  • 7
  • 51
  • 81
  • Remember: NSOperations are simply an API built on top of Grand Central Dispatch. So when you’re using NSOperations, you’re still using Grand Central Dispatch. It’s just that NSOperations give you some additional features that you might use. You can make some operations dependent on other operations, reorder queues after you sumbit items, re-use operations, cancel or suspend them, add dependency among various operations. – NSPratik Sep 12 '14 at 12:25