strong reference cycle in block
However, because the reference is weak, the object that self points to could be deallocated while the
block is executing.
You can eliminate this risk by creating a strong local reference to self inside the block:
__weak BNREmployee *weakSelf = self; // a weak reference
myBlock = ^{
BNREmployee *innerSelf = weakSelf; // a block-local strong reference
NSLog(@"Employee: %@", innerSelf);
};
By creating the strong innerSelf reference, you have again created a strong reference cycle between
the block and the BNREmployee instance. But because the innerSelf reference is local to the scope of
the block, the strong reference cycle will only exist while the block is executing and will be broken
automatically when the block ends.
This is good programming practice whenever you write a block that must reference self.
If you use an instance variable directly within a block, the block will capture self instead of the
instance variable. This is because of a little-known nuance of instance variables. Consider this code
that accesses an instance variable directly:
__weak BNREmployee *weakSelf = self;
myBlock = ^{
BNREmployee *innerSelf = weakSelf; // a block-local strong reference
NSLog(@"Employee: %@", innerSelf);
NSLog(@"Employee ID: %d", _employeeID);
};
The compiler interprets the direct variable access like this:
__weak BNREmployee *weakSelf = self;
myBlock = ^{
BNREmployee *innerSelf = weakSelf; // a block-local strong reference
NSLog(@"Employee: %@", innerSelf);
NSLog(@"Employee ID: %d", self->_employeeID);
};
Does the -> syntax look familiar? It is the syntax for accessing the member of a struct on the heap. At
their deepest darkest cores, objects are actually structs.
Because the compiler reads _employeeID as self->_employeeID, self is unexpectedly captured by the
block. This will cause the same strong reference cycle that you avoided with the use of weakSelf and
innerSelf.
The fix? Don’t access instance variables directly. Use your accessors!
__weak BNREmployee *weakSelf = self;
myBlock = ^{
BNREmployee *innerSelf = weakSelf; // a block-local strong reference
NSLog(@"Employee: %@", innerSelf);
NSLog(@"Employee ID: %d", innerSelf.employeeID);
};
Now there is no direct use of self, so there is no unintentional strong reference cycle. Problem solved.
strong reference cycle in block的更多相关文章
- Avoid strong reference cycles
转自:http://masteringios.com/blog/2014/03/06/avoid-strong-reference-cycles/ With the introduction of A ...
- Resolving Strong Reference Cycles for Closures
You resolve a strong reference cycle between a closure and a class instance by defining a capture li ...
- Java中引用类 strong reference .SoftReference 、 WeakReference 和 PhantomReference的区别
当在 Java 2 平台中首次引入 java.lang.ref 包,其中包含 SoftReference . WeakReference 和 PhantomReference 三个引用类,引用类的 ...
- 避免在block中循环引用(Retain Cycle in Block)
让我们长话短说.请参阅如下代码: - (IBAction)didTapUploadButton:(id)sender { NSString *clientID = @"YOUR_CLIENT ...
- java中的强引用(Strong reference),软引用(SoftReference),弱引用(WeakReference),虚引用(PhantomReference)
之前在看深入理解Java虚拟机一书中第一次接触相关名词,但是并不理解,只知道Object obj = new Object()类似这种操作的时候,obj就是强引用.强引用不会被gc回收直到gc roo ...
- Objective-C官方文档翻译 Block
版权声明:原创作品,谢绝转载!否则将追究法律责任. 一个Objective-c类定义了一个对象结合数据相关的行为.有时候,这使得他有意义的表达单个任务或者单元的行为.而不是集合的方法. blocks是 ...
- strong & weak
Here is a quick summary: A strong reference will keep the object it points to from being deallocated ...
- 《Programming with Objective-C》第八章 Working with Blocks
Blocks are Objective-C objects, which means they can be added to collections like NSArray or NSDicti ...
- Objective-C Programming The Big Nerd Ranch Guide 笔记 19-37
Properties are either atomic or nonatomic, The difference has to do with multithreading. atomic is t ...
随机推荐
- Python学习笔记-字符串
Python之使用字符串 1.所有的标准序列操作(索引,分片,乘法,判断成员资格,求长度,取最小值,最大值)对字符串同样适用.但是字符串都是不可变的. 2.字符串格式化使用字符串格式化操作符即%. f ...
- 《大象-Think In UML》读书笔记1
大音希声,大象希行. 什么是面向过程?什么是面向对象? 面向过程归纳为结构化程序设计.DFD图.ER模型.UC矩阵等,而面向对象则被归纳为继承.封装.多态.复用等具体的技术.事实上,上述的所有技术都只 ...
- Excel Access 新建空白文档/打开已有文档 提示内存或磁盘空间不足的解决方法--验证
服务器上发现,打开mdb数据库,点知道只有个空白的截面,打开已有的excel文件,一样,但多了个提示:内存磁盘空间不足或者关闭不再使用的工作表或者程序.检查过,内存和磁盘很充裕啊.那里不足啊,任务管理 ...
- hive的Query和Insert,Group by,Aggregations(聚合)操作
1.Query (1)分区查询 在查询的过程中,采用那个分区来查询是通过系统自动的决定,但是必须是在分区列上基于where子查询. SELECT page_views.* FROM page_view ...
- int a
系统编译之后所有的变量都存储到符号表中,并且每个表项被分配一个符号ID,一般也是数字的,可以根据该符号的ID直接访问符号的值内存中的数据都是二进制的,没有ASCII值ASCII只在为便于人的理解,对二 ...
- Joomla 文件操作常用方法
今天介绍下joomla下文件操作常用方法,这些方法在文件读写,图片文件上传,等都有用处. jimport('joomla.filesystem.file'); $j = new JFile(); ge ...
- js限制文本框只能输入整数或者带小数点[转]
这篇文章是关于js限制文本框只能输入整数或者带小数点的内容,以下就是该内容的详细介绍. 做表单验证的时候是否会碰到验证某个输入框内只能填写数字呢,仅允许输入整数数字或者带小数点的数字.下面这段代码也许 ...
- HDU 3652 B-number
也是数位dp.考虑反面会简单很多. #include<iostream> #include<cstdio> #include<cstring> #include&l ...
- PHP操作Excel – PHPExcel 基本用法详解
导出excel属性设置//Include classrequire_once('Classes/PHPExcel.php');require_once('Classes/PHPExcel/Writer ...
- Svn服务器的安装和配置
1.安装svn服务器端软件 从镜像服务器或者YUM源下载安装SVN服务器软件:yum install subversion mkdir /usr/local/svn //创建SVN安装目录 c ...