Warning solution for block's retain cycle

Source: Internet
Author: User

First, the block's retain cycle compilation warning only appears in the ARC project.

A solution here: http://blog.csdn.net/itianyi/article/details/8715857

I personally don't feel very good, so I checked the apple documentation and described it as follows:

In manual reference counting mode,__block id x;Has the effect of not retainingx. In Arc
Mode,__block id x;Defaults to retainingx(Just like all other values). To get the manual
Reference counting mode behavior under Arc, you cocould use__unsafe_unretained __block id x;. As the name__unsafe_unretainedImplies,
However, having a non-retained variable is dangerous (because it can dangle) and is therefore discouraged. Two better options are to either use__weak(If you don't need
Support IOS 4 or OS X v10.6), or set__blockValuenilTo break the retain cycle.

The following code fragment extends strates this issue using a pattern that is sometimes used in manual reference counting.

MyViewController *myController = [[MyViewController alloc] init…];
// ...
myController.completionHandler =  ^(NSInteger result) {
   [myController dismissViewControllerAnimated:YES completion:nil];
};
[self presentViewController:myController animated:YES completion:^{
   [myController release];
}];

As described, instead, you can use__blockQualifier and setmyControllerVariable
TonilIn the completion handler:

MyViewController * __block myController = [[MyViewController alloc] init…];
// ...
myController.completionHandler =  ^(NSInteger result) {
    [myController dismissViewControllerAnimated:YES completion:nil];
    myController = nil;
};

Alternatively, you can use a temporary__weakVariable. The following example extends strates a simple implementation:

MyViewController *myController = [[MyViewController alloc] init…];
// ...
MyViewController * __weak weakMyViewController = myController;
myController.completionHandler =  ^(NSInteger result) {
    [weakMyViewController dismissViewControllerAnimated:YES completion:nil];
};

For non-trivial cycles, however, you shoshould use:

MyViewController *myController = [[MyViewController alloc] init…];
// ...
MyViewController * __weak weakMyController = myController;
myController.completionHandler =  ^(NSInteger result) {
    MyViewController *strongMyController = weakMyController;
    if (strongMyController) {
        // ...
        [strongMyController dismissViewControllerAnimated:YES completion:nil];
        // ...
    }
    else {
        // Probably nothing...
    }
};

I personally prefer the method in the Apple documentation: using a temporary variable with weak reference, or using a block variable with a post-Nil

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.