0

Usually, Xcode shows a warning when using a strong reference in a block (retain cycle). However, I don't understand why it doesn't show it with this AFNetworking example.

UIImageView *imageView;
AFHTTPRequestOperation *operation = [apiQueryManager HTTPRequestOperationWithRequest:request success:^(AFHTTPRequestOperation *operation, NSData *responseObject) {
     UIImage *image = [UIImage imageWithData:responseObject];
     imageView.image =image;  // <--- using strong ref to imageView ?
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
     NSLog(@"ERROR: %@", error);
}];
[apiQueryManager enqueueHTTPRequestOperation:operation];

Is there a retain cycle here?

Amar
  • 13,202
  • 7
  • 53
  • 71
Noé Malzieu
  • 2,530
  • 3
  • 22
  • 27

1 Answers1

2

To have a retain cycle because of imageView, imageView would need to have a strong reference to the block in which it is used. This is not the case.

jbat100
  • 16,757
  • 4
  • 45
  • 70
  • 1
    In your example, Noe, a retain cycle would occur if `operation` is used within the block. – Guy Kogus Nov 15 '13 at 10:08
  • Ok I get it now :) Thank you both And by the way, I think AFNetworking gives the parameters of the success block as weak references (if I'm not mistaken), so, @GuyKogus, there would be no retain cycle ? – Noé Malzieu Nov 15 '13 at 10:12
  • A retain cycle often occurs when an object retains a block which retains that object (sounds confusing, I know). When you call a block, the parameters being passed into it are not retained in the block object (yes, blocks are objects), so there's no retain cycle. – Guy Kogus Nov 15 '13 at 10:27