gpt4 book ai didi

ios - 如何在 ios 中停止 dispatch_queue?

转载 作者:可可西里 更新时间:2023-11-01 17:17:28 25 4
gpt4 key购买 nike

我想知道当我点击导航栏中的后退按钮时停止异步任务的方法。我已经完成了这段代码,但它不起作用......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 适用于即发即弃的任务。如果您想要可取消的任务,我建议您使用 NSOperationNSOperationQueue

关于ios - 如何在 ios 中停止 dispatch_queue?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25787477/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com