Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios NSOperationQueue未被取消_Ios_Objective C_Nsoperationqueue_Nsthread - Fatal编程技术网

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]的目的是什么?你看过我发布的链接了吗?它显示了如何在操作块内检查取消。