避免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下,解决上述强引用环的方法如下:

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

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

 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...
}
};

避免Block中的强引用环的更多相关文章

  1. Block内的强引用

    众所周知,当某个对象持有着一个Block的时候,如果在Block内部使用强引用反过来持有这个对象,就会导致引用循环.为了避免引用循环,可以使用__weak修饰符,苹果的官方文档在用代码演示__weak ...

  2. Java中的强引用,软引用,弱引用

    作者:winterSunshine链接:https://www.zhihu.com/question/37401125/answer/100981172来源:知乎著作权归作者所有.商业转载请联系作者获 ...

  3. java中的强引用、软引用、弱引用、虚引用

    1.强引用(Strong Reference):指程序代码中普遍存在的,类似“Object obj = new Object()”这类的引用,只要对象存在强引用关联,JVM必定不会回收这个对象: 2. ...

  4. java中的强引用(Strong reference),软引用(SoftReference),弱引用(WeakReference),虚引用(PhantomReference)

    之前在看深入理解Java虚拟机一书中第一次接触相关名词,但是并不理解,只知道Object obj = new Object()类似这种操作的时候,obj就是强引用.强引用不会被gc回收直到gc roo ...

  5. Java中的强引用和弱引用

    旭日Follow_24 的CSDN 博客 ,全文地址请点击: https://blog.csdn.net/xuri24/article/details/81114944 一.强引用 如下是强引用的经典 ...

  6. block中防止循环引用的一个高大上的宏定义

    看惯了什么tempSelf weakSelf,来点高大的 #define weakify(...) \ rac_keywordify \ metamacro_foreach_cxt(rac_weaki ...

  7. Block中的循环引用警告

  8. block中如何避免循环引用

    使用 weak–strong dance 技术 block 可以直接引用 self,但是要非常小心地在 block 中引用 self.因为在 block 引用 self,可能会导致循环引用.如下例所示 ...

  9. dispatch_async 的 block 中是否该使用_weak self

    问题分析 我看过很多文章关于在dispatch_async的block里面使用_weak self, 但是让我疑惑的是,以下代码是否需要必须使用_weak self, 因为我也看到了很多观点说,在有些 ...

随机推荐

  1. 基于ffmpegSDK的开发

    #include <stdio.h> #include <libavutil/avutil.h> #include <libavcodec/avcodec.h> # ...

  2. AngularJS资源大集锦

    AngularJS最近貌似很火,前段时间,CSDN的编辑专访了AngularJS创始人Misko Hevery.这不,Tuts+网站编辑Rey Bango应广大读者需要,把各种极好的AngularJS ...

  3. OpenLTE安装教程

    安装需求: USB3 interface Modern multicore CPU (Intel Core i5, Core i7 or equivalent with SSE4.1 SSE4.2 a ...

  4. C# 基础备忘录

    1. decimal 类型调用ToString()方法后没把末尾的0去掉的解决办法: 例子:decimal? money = Convert.ToDecimal(10.8950); string mo ...

  5. servlet配置及其生命周期

    servlet配置: 在web.xml中,首先向服务器注册一个servlet.在<servlet>标签下 给定一个servlet名字,这个servlet-name是我们自己用的,方便我们用 ...

  6. Java renameTo()重新命名此抽象路径名表示的文件

    Java手册 renameTo public boolean renameTo(File dest) 重新命名此抽象路径名表示的文件. 此方法行为的许多方面都是与平台有关的:重命名操作无法将一个文件从 ...

  7. Linux新手入门:Unable to locate package错误解决办法

    最近刚开始接触Linux,在虚拟机中装了个Ubuntu,当前的版本是Ubuntu 11.10,装好后自然少不了安装一些软件,在设置了软件的源后,就开始了 sudo apt-get install,结果 ...

  8. mysql5.7.12/13在安装新实例时报错:InnoDB: auto-extending data file ./ibdata1 is of a different size 640 pages (rounded down to MB) than specified in the .cnf file: initial 768 pages, max 0 (relevant if non-zero

    .bin/mysqld --initialize-insecure --basedir=xxx --datadir=xxx 然后 .bin/mysqld_safe --defaults-file=xx ...

  9. APR介绍

    http://blog.csdn.net/jmshl/article/details/6773731 APR分析-整体篇 由于部门所使用的底层库与Apache Server有着“一定的渊源”,所以总有 ...

  10. python requests 爬取数据

    import requests from lxml import etree import time import pymysql import json headers={ 'User-Agent' ...