我想知道当我点击导航栏中的后退按钮时停止异步任务的方法。我已经完成了这段代码,但它不起作用...... 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];
}
});
在视野中消失....
-(void)viewDidDisappear:(BOOL)animated
{
dispatch_suspend(imageQueue);
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"Avatardownloaded" object:nil];
[[NSNotificationCenter defaultCenter]release];
[avatarimages release];
[imagedata release];
[imageURL release];
}
即使我挂起线程,它也不会停止执行,它会继续在后台运行。任何人都请帮助我
最佳答案
Grand Central Dispatch 适用于即发即弃的任务。如果您想要可取消的任务,我建议您使用 NSOperation
和 NSOperationQueue
。
关于ios - 如何在 ios 中停止 dispatch_queue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25787477/