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 ....
-(void)viewDidDisappear:(BOOL)animated
{
dispatch_suspend(imageQueue);
[[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
+3
source to share
4 answers
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.
0
source to share