nst / STHTTPRequest

Obj-C / Cocoa HTTP requests for humans
BSD 3-Clause "New" or "Revised" License
825 stars 82 forks source link

Capturing 'self' strongly in this block is likely to lead to a retain cycle #12

Closed cri1682 closed 11 years ago

cri1682 commented 11 years ago

I'm getting this warning if i call "self" inside the completion block or the error block, something like

     conn.errorBlock = ^(NSError *error) {

        if (error.code!=4 ) {
            UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"error" message:@"" delegate:self cancelButtonTitle:@"close" otherButtonTitles:nil];
            [alert show];
        }

    };

The warning is attached on "delegate:self". The warning disappear if i declare a weak reference to "self"

__weak MyDataProcessor *dp = self;

Maybe you can prevent those warnings by an intervention in the library?

nst commented 11 years ago

Unfortunately, I don't think I can do anything.

Capturing a weak self as you do is, according to Apple, a "best practice":

https://developer.apple.com/library/ios/documentation/cocoa/conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html