NSBlockoperation没有被取消

时间:2012-09-12 19:43:23

标签: objective-c ios asynchronous uitableview nsblockoperation

我有一个uitableview,显示每个单元格中的图像,这些图像可以在线下载。

要使此调用异步,我使用NSBlockoperation。我更喜欢使用它,因为我之前使用过GCD,但你无法取消GCD。原因是如果我离开视图,图像会在App的背景下载,当我再次进入上一个视图时,GCD会让它再次排队,所以最终会有一整堆图像和用户永远不会看到uitableview。这就是为什么我选择NSBlockoperation。

但是,我的积木不会被取消。这是我使用的代码(它是 - (void)tableView的一部分:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {):

// Create an operation without any work to do
    downloadImageOperation = [NSBlockOperation new];

    // Make a weak reference to the operation. This is used to check if the operation
    // has been cancelled from within the block
    __weak NSBlockOperation* operation = downloadImageOperation;


    // Give the operation some work to do
    [downloadImageOperation addExecutionBlock: ^() {
        // Download the image
        NSData *data = [NSData dataWithContentsOfURL:[newsimages objectAtIndex:indexPath.row]];;
        UIImage *image = [[UIImage alloc] initWithData:data];
         NSLog(@"%@",image);
        // Make sure the operation was not cancelled whilst the download was in progress
        if (operation.isCancelled) {
            return;
            NSLog(@"gestopt");
        }
        if (image != nil) {

            NSData* imageData = UIImagePNGRepresentation(image);

            [fileManager createFileAtPath:path contents:imageData attributes:nil];
            cell.imageView.image = image;
            cell.imageView.layer.masksToBounds = YES;
            cell.imageView.layer.cornerRadius = 15.0;

        }

        // Do something with the image
    }];

    // Schedule the download by adding the download operation to the queue
    [queuee addOperation:downloadImageOperation];

我已使用此代码取消:

-(void)viewDidDisappear:(BOOL)animated {
[downloadImageOperation cancel];
}

然而,我的NSLog告诉我,即使我的观点消失了(我把一个nslog放在那里),仍然有块。

 2012-09-12 21:32:31.869 App[1631:1a07] <UIImage: 0x3965b0>
 2012-09-12 21:32:32.508 App[1631:1907] <UIImage: 0x180d40>
 2012-09-12 21:32:32.620 App[1631:707] view dissappear!
 2012-09-12 21:32:33.089 App[1631:3a03] <UIImage: 0x3a4380>
 2012-09-12 21:32:33.329 App[1631:5a03] <UIImage: 0x198720>

注意:视图中每次都会显示4个单元格,所以我认为即使我离开了视图,它们仍然在队列中..

1 个答案:

答案 0 :(得分:1)

看来您的代码从不排队多个块 - 这是正确的吗?如果没有,则需要将“取消”发送到队列而不是操作。

无论如何,你的问题很可能是这一行:

NSData *data = [NSData dataWithContentsOfURL:[newsimages objectAtIndex:indexPath.row]];;

正在为您同步进行下载 - 因此,如果您在此消息之后发送'取消',则很长时间内都不会看到取消。这就是为什么大多数开发人员使用异步NSURLConnections进行并发NSOperations进行下载的原因 - 这样就可以实时获取取消消息。

假设这是ARC,您可以在dealloc中添加日志以验证操作已完成或已取消,并且已正确释放。 [注意上面的日志是在返回之后所以永远不会被调用。你还应该留下更多isCancelled信息,这样你就可以在取消后尽快停止。]

相关问题