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可以作为函数参数或者函数的返回值,而其本身又可以带输入参数或返回值.它和传统的函数指针很 ...
随机推荐
- python开发_thread_线程基础
说到线程,我们要知道啥是串行,啥是并行程序 举个例子: 串行程序,就是一个一个的执行程序 #python threading import time ''' 每一秒中,输出:this is a dem ...
- 记一次完整的pc前端整站开发
我所做的项目是一个线上的旅游平台,当然不是大家耳熟能详的那些旅游平台了,项目刚开始也没有必要去评价它的好坏,在这里我就不多说了~这是线上地址有兴趣的同学可以去看看(www.bookingctrip.c ...
- Null 和 Undefined
在JavaScript中存在这样两种原始类型:Null与Undefined.这两种类型常常会使JavaScript的开发人员产生疑惑,在什么时候是Null,什么时候又是Undefined? Undef ...
- DeveloperAppleHelp
UIKit: 1.UIKit User Interface Catalog 视图 View控件 2.View Programming Guide for iOS 视图编程,用代码 构建界面. 3. ...
- org.springframework.orm.hibernate3.LocalSessionFactoryBean
Spring整合hibernate在配置sessionFactory时, 启动总是报出javax.transaction.TransactionManager找不到. 原因是:缺少jar包,jta-1 ...
- php 获取开始日期与结束日期之间所有日期
话不多说,源码奉上! function getDateRange($startdate, $enddate) { $stime = strtotime($startdate); $etime = st ...
- Ubuntu 11.10 (Oneiric)上编译带utrace补丁的内核 转
Ubuntu 11.10 (Oneiric)上编译带utrace补丁的内核 首先准备linux内核编译环境: sudo apt-get install fakeroot build-essential ...
- setTimeout() 实现程序每隔一段时间自己主动运行
定义和使用方法 setTimeout() 方法用于在指定的毫秒数后调用函数或计算表达式. 语法 setTimeout(code,millisec) 參数 描写叙述 code 必需.要调用的函数后要运行 ...
- Android Binder分析二:Natvie Service的注冊
这一章我们通过MediaPlayerService的注冊来说明怎样在Native层通过binder向ServiceManager注冊一个service,以及client怎样通过binder向Servi ...
- vc中nmake.exe cl.exe 的使用
首先简单介绍一下程序是如何编译链接的.程序写好之后,我们进行编译和链接来产生可执行程序.这时候,编译器为了完成编译和链接,需要知道很多信 息.比如要编译的文件是哪一个,使用哪些编译选项进行编译,编译好 ...