[NSURLConnection sendAsynchronousRequest:req queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    dispatch_async(dispatch_get_main_queue(), ^{
        if(data != nil && self.superview != nil) { // <-- EXC_BAD_ACCESS on this line
            [self.musicItemImg setAlpha:0.0];
            [self.musicItemImg setImage:[UIImage imageWithData:data]];
            [UIView animateWithDuration:0.25 animations:^{
                [self.musicItemImg setAlpha:1.0];
            }];
        }
    });
}];


有时,在异步加载完成之前删除此视图,并在尝试使用self时得到一个EXC_BAD_ACCESS,这是可以理解的。我可以中止异步连接吗?还是有更好的方法来解决这个问题?

最佳答案

首先,在UIView子类中启动url请求是非常糟糕的样式。您应该在视图控制器中执行此操作!

NSOperationQueue应该是视图控制器的类实例。同样,您需要在完成处理程序中配置的视图也必须是类实例(属性)。对所有请求使用相同的操作队列。在viewWillDisappear中,您可以使用以下命令取消所有请求操作:

[self.operationQueue cancelAllOperations];

关于ios - ios exc_bad_access由于延迟发送,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11091514/

10-14 17:39
查看更多