• 避免Block中的强引用环


    避免Block中的强引用环

      In manual reference counting mode, __block id x; has the effect of not retaining x. In ARC mode, __block id x; defaults to retaining x (just like all other values). To get the manual reference counting mode behavior under ARC, you could use __unsafe_unretained __block id x;. As the name __unsafe_unretained implies, 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 to support iOS 4 or OS X v10.6), or set the __block value to nil to break the retain cycle.

      例如,在MRC&ARC下,以下代码会存在强引用环:

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

      在MRC下,解决上述强引用环的方法如下:

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

      在ARC下,解决上述强引用环的方法如下:

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

      更好的方式是在使用__weak变量前,先用__strong变量把该值锁定,类似使用weak_ptr一样。

     1 MyViewController *myController = [[MyViewController alloc] init…];
     2 // ...
     3 MyViewController * __weak weakMyController = myController;
     4 myController.completionHandler =  ^(NSInteger result) {
     5     MyViewController *strongMyController = weakMyController;
     6     if (strongMyController) {
     7         // ...
     8         [strongMyController dismissViewControllerAnimated:YES completion:nil];
     9         // ...
    10     }
    11     else {
    12         // Probably nothing...
    13     }
    14 };
  • 相关阅读:
    Codeforces Round #124 (Div. 2)
    Codeforces Round #158 (Div. 2)
    Codeforces round FF
    缕缕,
    拉伸 原来 就这一句话,
    tableveiw上面 的手势,
    reloaddata 是没有对 tableview的 headerview进行 刷新的,
    y
    考虑欠缺 以及 设计 导致的 工作量,
    present出来的 controller上的 controller里面用 navigationcontrolle了,失效了,
  • 原文地址:https://www.cnblogs.com/tekkaman/p/3746259.html
Copyright © 2020-2023  润新知