简体   繁体   English

弱自我

[英]WeakSelf in blocks

Got a question about weak self , blocks and retain cycle. 有一个关于自我弱小的问题,阻止并保持生命周期。

By the book i understand we need to use weakself in blocks.. The question is , when ? 通过这本书,我了解到我们需要在块中使用弱势自我。问题是,什么时候?

for example, simple animation code, never contain weakself.. 例如简单的动画代码,从不包含弱点。

ie

self.myView.alpha = 1.0;
[UIView animateWithDuration:0.2 animations:^{
self.myView.alpha = 1.0; 
}];

is this code ok ? 这个代码可以吗? or should i create a weakself before the block and use it inside ? 还是我应该在块之前创建一个弱者并在内部使用它?

In all my code and all other projects i ever worked on, never saw a single line that uses weak self. 在我从事过的所有代码和所有其他项目中,从未见过使用弱自我的一行。 i'm now trying to use weakself in every block.. it's just that i'm not sure it's necessary 我现在正尝试在每个块中使用弱点。。只是我不确定是否有必要

Looking forward for your opinions Thanks 期待您的意见谢谢

You should use weak selfs when there is a possibility of retain cycles. 如果存在保留周期,则应使用弱自我。

Imagine an instance of foo has a strong reference to bar. 想象一下foo的实例对bar有很强的引用。 Now you give bar a block with references to foo's self. 现在,您为bar提供一个引用foo自身的块。 Now someone releases foo, but bar has kept the block around. 现在有人释放了foo,但是bar阻止了它。 Now foo has a strong reference to bar and bar holds a strong reference to foo in the block. 现在foo对bar有很强的引用,而bar在块中对foo有很强的引用。 Foo will not be released, and therefore also bar, as bar is holding on to it. Foo将不会被释放,因此也不会释放,因为bar会一直坚持下去。 But the only thing holding on to bar is the now unused foo. 但是唯一要坚持的是现在未使用的foo。 You've got yourself a retain cycle, and the two objects are now floating in memory unable to be reached. 您已经拥有了一个保留周期,并且两个对象现在都无法在内存中浮动。

UIView's animations pose no problem, as the block is called before the animate: method returns, and UIView does not keep the block around. UIView的动画没有问题,因为在animate:方法返回之前调用了该块,并且UIView并未保留该块。

ARC will usually warn you whenever it sees the possibility of a retain cycle. ARC通常会在发现保留周期的可能性时向您发出警告。 But that's not always the case. 但这并非总是如此。 A good rule of thumb is to use weak selfs whenever you don't know where the block will en up. 一个好的经验法则是,只要您不知道该障碍物将在何处出现,就使用弱的自我。

Hope this is a bit of help. 希望这会有所帮助。

As others have pointed out, you should definitely use the weakSelf pattern in situations where you would otherwise have strong reference cycles (aka, retain cycles). 正如其他人指出的那样,在否则您将拥有较强的参考周期(又称为保留周期)的情况下,绝对应该使用weakSelf模式。 But more generally, you should use weakSelf whenever you don't want the block to retain the object itself (even in cases where there is no retain cycle involved). 但是更普遍的是,每当您不希望块保留对象本身时(即使在不涉及保留周期的情况下),也应使用weakSelf

A wonderful example would be a network operation that is initiated by some view controller. 一个很好的例子是由某个视图控制器启动的网络操作。 Let's say the user initiates some upload. 假设用户启动了一些上传。 The question is whether you want the asynchronous upload process to retain the view controller even though it might have a reference to that view controller to update some progress bar or the like. 问题是您是否希望异步上传过程保留视图控制器,即使它可能引用该视图控制器来更新某些进度条等。 You may well not want it to retain the view controller if the view controller is dismissed, even though you might want to let the upload continue. 即使关闭了视图控制器,您也可能不希望它保留视图控制器,即使您可能希望继续上传。

This is just a random example, but bottom line, you might want to use weakSelf pattern whenever you want the background process to continue, but you don't want it retaining the other object. 这只是一个随机的示例,但最重要的是,您可能希望在后台进程继续进行时每次都使用weakSelf模式,但又不想保留其他对象。 Just look at your functional needs and consider the strong reference cycle risks, and make the decision whether you need to employ the weakSelf pattern or not. 只需查看您的功能需求并考虑强大的参考周期风险,然后决定是否需要采用weakSelf模式即可。

In the case of animateWithDuration , the animation stops when the view is dismissed and the strong reference is immediately resolved, so there is no strong reference cycle. animateWithDuration的情况下,当关闭视图并立即解析强参考时,动画将停止,因此不存在强参考周期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM