0
[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?

Jacksonkr
  • 31,583
  • 39
  • 180
  • 284

1 Answers1

2

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:

[self.operationQueue cancelAllOperations];
Felix
  • 35,354
  • 13
  • 96
  • 143
  • I believe you, but what's the explanation behind controllers being the preference for url requests? – Jacksonkr Jun 18 '12 at 22:46
  • 2
    Thats just the basics of the MVC design pattern. Views should only display data, which is passed from the controller. Also views can be unloaded in low memory conditions - controllers are more lightweight. – Felix Jun 18 '12 at 22:56