How to stop dispatch_queue in ios?

I want to know how to stop an asynchronous task when I click the back button in the navigation bar. I did 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 ....


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


although i pause the thread it doesnt stop its keepon running in background.Anyone pls help me


source to share

4 answers

Grand Central Dispatch is suitable for tasks involving fire and forgetting. If you want to cancel tasks, I would recommend using NSOperation

and perhaps NSOperationQueue




No, you cannot do this. Use NSTimer instead.



dispatch_queue does not support cancellation. However, you can use a block variable or some global variable to track the cancellation in your dispatch_queue code. The code in your dispatch_queue should be able to terminate and cancel = YES (for example).



There are facilities such as dispatch_suspend () (along with dispatch_resume ()). This will prevent any new blocks from being scheduled in a specific queue. Blocks already running will not be affected . If you want to pause an already scheduled and running block, you need to check some conditions and pause the code execution. To suspend queues / cancel operations, you must use NSOperation and NSOperationQueue instead of GCD.



All Articles