What the difference between __weak and __block reference?
近日遇到一个非常细的知识点,关于block的循环引用问题。相比非常多人都遇到了。也能顺利攻克了,至于block方面的技术文章。那就很多其它了。这里不再赘述,可是有这样一个问题:
What the difference between __weak and __block reference?
使用场景:
__block typeof(self) tmpSelf = self;
[self methodThatTakesABlock:^ {
[tmpSelf doSomething];
}];
那么这样写:
__block typeof(self) tmpSelf = self;
和
__weak typeof(self) tmpSelf = self;
写有什么不同,可能測试过的同学觉的效果都一样。那么究竟有什么不同呢?
先看官方解释:
From the docs about __block
__block variables live in storage that is shared between the lexical scope of the variable and all blocks and block copies declared or created w
So they are technically different things. __block is to stop your variable being copied from your external scope into your block scope. __weak is a self delimiting weak pointer.
From the docs about __weak
__weak specifies a reference that does not keep the referenced object alive. A weak reference is set to nil when there are no strong references to the object.
中文意思。请大家自行理解,我们来看大家的解释。纵观国内技术文章,基本都是一说一大片。发现真正没有解决之前的疑问,有什么不同,好了。先看以下的这段话:
So they are technically different things. __block is to stop your variable being copied from your external scope into your block scope. __weak is a self delimiting weak pointer.
Note I said technically, because for your case they will do (almost) the same thing. The only difference is if you are using ARC or not. If your project uses ARC and is only for iOS4.3 and above, use __weak. It ensures the reference is set to nil if the global
scope reference is releases somehow. If your project doesn't use ARC or is for older OS versions, use __block.
There is a subtle difference here, make sure you understand it.
EDIT: Another piece to the puzzle is __unsafe_unretained. This modifier is almost the same as __weak but for pre 4.3 runtime environments. HOWEVER, it is not set to nil and can leave you with hanging pointers.
总结例如以下:
AIn 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下,我们通常使用__block 。而在ARC下我们通常使用__weak , 或者__unsafe_unretaine __block(不安全,不建议使用) 来修饰对象防止循环引用而造成的内存泄露。
What the difference between __weak and __block reference?的更多相关文章
- IOS开发 __weak与__block修饰符到底有什么区别
API Reference对__block变量修饰符有如下几处解释: //A powerful feature of blocks is that they can modify variables ...
- __weak与__block区别分析
API Reference对__block变量修饰符有如下几处解释: //A powerful feature of blocks is that they can modify variables ...
- __weak 和 __block 区别
Blocks理解: Blocks可以访问局部变量,但是不能修改 如果修改局部变量,需要加__block __block int multiplier = 7; int (^myBlock)(int) ...
- __weak与__block修饰符区别
API Reference对__block变量修饰符的解释,大概意思: 1.__block对象在block中是可以被修改.重新赋值的. 2.__block对象在block中不会被block强引用一次, ...
- __weak与__block区别,深层理解两者区别
准备工作 首先我定义了一个类 MyObject 继承 NSObject,并添加了一个属性 text,重写了description方法,返回 text 的值.这个主要是因为编译器本身对 NSString ...
- iOS __weak 和 __block 的使用探讨
在基本的开发中遇到 需要弱引用时候 我一般 用 weak 预防 死锁的时候 我会用 block 的确没出过大错 但是这样处理 的确有点囫囵 现在我想好好理解一下这两个修饰符 "bloc ...
- __Block与__Weak区别
一.__block理解: Blocks可以访问局部变量,但是不能修改, 声明block的时候实际上是把当时的临时变量又复制了一份, 在block里即使修改了这些复制的变量,也不影响外面的原始变量.即所 ...
- __block和__weak的区别
API Reference对__block变量修饰符有如下几处解释: //A powerful feature of blocks is that they can modify variables ...
- __block __weak
IOS BLOCK收集 在ios,blocks是对象,它封装了一段代码,这段代码可以在任何时候执行.Blocks可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值.它和传统的函数指针很 ...
随机推荐
- 【51Nod 1222】最小公倍数计数
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1222 求\([a,b]\)中的个数转化为求\([1,b]\)中的个数减去 ...
- [SimpleOJ229]隧道
题目大意: 有10个格子,初始状态a和b分别在5和6上. 现在有n个任务,每个任务都有特定的位置. 在每个单位时间,a和b可以分别进行以下事件中的任意一件: 1.向左(右)移动一个格子: 2.锁定在当 ...
- 【BZOJ】4318: OSU!【期望DP】
4318: OSU! Time Limit: 2 Sec Memory Limit: 128 MBSubmit: 1473 Solved: 1174[Submit][Status][Discuss ...
- OpenGL 模型视图投影矩阵 仿射矩阵
矩阵基础知识 要对矩阵进行运算,必须先要了解矩阵的计算公式,这个知识的内容涉及到了线性代数. 我们知道在Cocos2dx中,有关于平移,旋转,缩放等等操作,都必须要进行矩阵的乘法. 只需要一张图就能理 ...
- phalcon Model 'partitions' could not be loaded(模型不支持分区语句)
注意: 很明确提示用phalcon自带的模型层是不能用partition这个关键字的 解决方法: 自己写个PDO类 然后用pdo中的query方法执行语句成功: mysql分区目的 是减少数据库的负担 ...
- 分布式文件系统 ~MogileFS~
一.分布式文件系统 分布式文件系统(Distributed File System)是指文件系统管理的物理存储资源不一定直接连接在本地节点上,而是通过计算机网络与节点相连,也就是集群文件系统,可以支持 ...
- 多线程_java多线程环境下栈信息分析思路
导读:Java多线程开发给程序带来好处的同时,由于多线程程序导致的问题也越来越多,而且对问题的查找和分析解决对于菜鸟程序原来是是件头疼的事.下面我就项目中使用多线程开发程序过程中遇到的问题做详细的分析 ...
- document.all理解
The all collection includes one element object for each valid HTML tag. If a valid tag has a matchin ...
- Tasker to detect and vibrate once the ougoing call is being answered
I happen to find that for GSM standard phone, call duration would be created into sql database only ...
- 5V and 3V Level Translators
http://www.daycounter.com/Circuits/Level-Translators/Level-Translators.phtml Interfacing 5V and 3V l ...