3

After updating to iOS 6.1, I'm getting this warning in AFImageRequestOperation.m and AFHTTPClient.m from AFNetworking framework:

Capturing 'operation' strongly in this block is likely to lead to a retain cycle

Based on this answer, I can fix a retain cycle in ARC by using __weak variables. It is also says

Block will be retained by the captured object

Does anyone know how to solve this?

Thanks.

Community
  • 1
  • 1
Maziyar
  • 1,913
  • 2
  • 18
  • 37

2 Answers2

4

We are fortunate that XCode 4.6 is showing a warning to avoid this problem It can be solved by providing a weak reference

AFImageRequestOperation *requestOperation = [[AFImageRequestOperation alloc] initWithRequest:urlRequest];

**__weak AFImageRequestOperation *tempRequestOperation = requestOperation;**

[requestOperation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    if (success) {
        UIImage *image = responseObject;
        if (imageProcessingBlock) {
            dispatch_async(image_request_operation_processing_queue(), ^(void) {
                UIImage *processedImage = imageProcessingBlock(image);

                dispatch_async(**tempRequestOperation**.successCallbackQueue ?: dispatch_get_main_queue(), ^(void) {
                    success(operation.request, operation.response, processedImage);
                });
            });
        } else {
            success(operation.request, operation.response, image);
        }
    }
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    if (failure) {
        failure(operation.request, operation.response, error);
    }
}];
splrs
  • 2,424
  • 2
  • 19
  • 29
Harini
  • 303
  • 2
  • 6
  • 14
  • Hi Sunny, I did the exact same thing but the warnings are still there. I even downloaded the latest AFNetworking framework with all the new commits because I was told that new commits solved these problems while ago but the warnings are still there. Warnings are here: AFHTTPCliend.m line #564 NSMutableArray *mutableOperations = [NSMutableArray array]; and AFImageRequestOperation.m line #85 dispatch_async(operation.successCallbackQueue ?: dispatch_get_main_queue(), ^(void) { success(operation.request, operation.response, processedImage); }); Btw, my app hasn't crashed or showed any problems. – Maziyar Feb 03 '13 at 03:43
  • I implemented the above solution and it worked for me. So do we have to download the latest version or solution that i provided will work. – Harini Feb 03 '13 at 14:28
0

OK here was the problem. I was keep downloading the Master branch from GitHub and now that I tried downloading AFNetworking from here (version 1.1.0) it doesn't show me the warning anymore.

I don't why the latest commits were not included in the master branch when I downloaded but clearly they've solved these strong refs in blocks warnings while ago.

Always check the website to see the latest released version or sync the latest commit from GitHub :) (It wasn't showing anything in my iOS 6.0 apps but Xcode 4.6 just brought them up)

Maziyar
  • 1,913
  • 2
  • 18
  • 37