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(不安全,不建议使用) 来修饰对象防止循环引用而造成的内存泄露。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值