[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];
}];
}
});
}];
Sometimes this view is removed before the async load is finished and when it tries to use self I get an EXC_BAD_ACCESS and understandably so. Can I abort an async connection? Or is there a better way to combat this?
First of all it is very bad style to start url requests in a UIView subclass. You should do that in a view controller!
The NSOperationQueue should be a class instance of the view controller. Also the views you need to configure in the completion handler must be class instances (properties). Use the same operation queue for all requests. In viewWillDisappear you can cancel all request operations with: