Ios NSOperationQueue未被取消
我正在使用Ios NSOperationQueue未被取消,ios,objective-c,nsoperationqueue,nsthread,Ios,Objective C,Nsoperationqueue,Nsthread,我正在使用PageViewController,其中可能有多个图像作为内容。我从服务中得到如下图像。但是,当用户在网络操作仍在进行时单击以关闭viewcontroller时,应用程序崩溃 queue = [[NSOperationQueue alloc] init]; operation = [NSBlockOperation blockOperationWithBlock:^{ [self addAllImages]; dispatch_
PageViewController
,其中可能有多个图像作为内容。我从服务中得到如下图像。但是,当用户在网络操作仍在进行时单击以关闭viewcontroller时,应用程序崩溃
queue = [[NSOperationQueue alloc] init];
operation = [NSBlockOperation blockOperationWithBlock:^{
[self addAllImages];
dispatch_sync(dispatch_get_main_queue(), ^(void) {
[self pageViewcontrollerSetup];
});
}];
[queue addOperation:operation];
}
- (void) addAllImages
{
self.pageImages =[[NSMutableArray alloc] initWithCapacity:self.pageControl.numberOfPages];
for(id key in [[[pElements objectAtIndex:self.selectedIndexPath.row]objectForKey:@"pdetail"] objectForKey:@"images"]) {
NSString *productURL = [NSString stringWithFormat:@"%@%@", PRODUCT_URL, [[[[pElements objectAtIndex:self.selectedIndexPath.row]objectForKey:@"pdetail"] objectForKey:@"images"] objectForKey:key]];
NSData* productData = [NSData dataWithContentsOfURL:[NSURL URLWithString:productURL]];
if ([UIImage imageWithData:productData]) {
[self.pageImages addObject:[UIImage imageWithData:productData]];
}
}
}
- (void)closeBtnClicked {
[queue cancelAllOperations];
}
注意:我使用的是
GCD
(Grand Central Dispatch)多线程,但我知道您无法取消它,因此我切换到NSOperationQueue
取消操作并不会神奇地使您的代码停止运行。由您在操作中的代码定期检查操作是否已取消,如果已取消,则退出操作。您能给我举个小例子吗?我以为cancelAllOperations
会立即取消相应的线程。如果不是,那么使用[queue cancelAllOperations]的目的是什么代码>?你看过我发布的链接了吗?它显示了如何在操作块内检查取消。