断裂保持周期与强/弱自我 [英] Breaking retain cycle with strong/weak self

查看:158
本文介绍了断裂保持周期与强/弱自我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读关于strong / weak self 的帖子来打破保留周期,但我仍然困惑他们如何工作。我理解使用 __ weak typeof(self)weakSelf = self 创建一个弱的引用自我,但我困惑的强引用。根据我的理解,强引用是有一个强有力的引用 self ,这样它不会在块结束之前被释放的权利?那么为什么需要 __ strong typeof(self)strongSelf = weakSelf ?这不是最终指向自我的对象吗?那么为什么不是 strongSelf = self

I've read posts about strong/weak self to break retain cycles but I am still confused as to how they work. I understand the use of __weak typeof(self) weakSelf = self to create a weak reference to self but I am confused about strong reference. As I understand it, the strong reference is so that there is a strong reference to self so that it doesn't get deallocated before the end of the block right? So why is it necessary to have __strong typeof(self) strongSelf = weakSelf? Doesn't this end up pointing to the self object anyway? So why not just strongSelf = self?

推荐答案

在块中引用的对象将在创建块时导致对该对象的隐式保留。未执行,但已创建。

Any non-weak object that you reference inside the block will result in an implicit retain on that object, as the block is being created. Not executed, but created.

如果您直接从self初始化内部strongSelf,您将保留self的值并可能导致保留周期。

If you initialised the inner strongSelf directly from self, you will retain the value of self and potentially cause a retain cycle.

另一方面,如果你从weakSelf初始化它,你将不会保留weakSelf的值。

On the other hand, if you initialise it from weakSelf, you will not retain the value of weakSelf.

这是两步骤的原因。外部代码将self的值复制到weakSelf中,但ARC不会添加一个retain,因为它是__weak()。

This is the reason for the two-step. The outer code copies the value of self into weakSelf, but ARC does not add a retain because it is __weak().

块creation复制weakSelf (或至少,设法使其值在执行时可用)。

The block "creation" copies the value of weakSelf (or at least, manages to makes its value available at execution time). You can't see where it copied it to, but it did.

在块执行时间,块复制weakSelf的值(这将是nil如果自己被释放在同时)into strongSelf,那么ARC然后应用一个retain。因此,在块的持续时间,由strongSelf引用的对象将保持活动状态,如果它是活的开始。如果你只依靠weakSelf,它可能在执行块期间的任何时候nil。

At block "execution" time, the block copies the "value of weakSelf" (which will be nil if self has been dealloc'ed in the mean time) into strongSelf which ARC then applies a retain to. Thus, for the duration of the block, the object referenced by strongSelf will remain alive, if it was alive to begin with. If you had only relied on weakSelf, it could go nil at any time during the execution of the block.

注意弱/强模式是腰带和大括号 - 许多示例实际上依赖于weakSelf将变为nil的事实,并且块将静默地变成无操作的集合(到nil的消息)。

Note that weak/strong pattern is belt-and-braces - many examples actually rely on the fact that the weakSelf will go nil, and the block will silently become a collection of no-ops (messages to nil).

保留周期只有在(a)你保留对self.property中的块的引用,或者(b)你把块移动到其他对象(通知管理器等),并告诉其他对象在你的dealloc中忘记它;

Retain cycles typically only occur if (a) you keep a reference to the block in a self.property or (b) you hand the block off to some other object (notification manager, etc), and tell that other object to forget it in your dealloc; in both cases your dealloc will never be called while the block is alive.

当人们说做这个东西的方法是弱/强模式时,正在假设最糟糕的情况。

When people say "the way to do this stuff is with the weak/strong pattern", they are assuming the worst possible scenario.

这篇关于断裂保持周期与强/弱自我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆