[英]Would this create a retain cycle?
I am learned about the retain cycle then I was watching this video tutorial but I felt the code he was writing contain retain cycle. 我了解了保留周期然后我正在观看这个视频教程,但我觉得他写的代码包含保留周期。 So I thought to post is here to ask opinion. 所以我想发帖就是在这里征求意见。 Below the code snippet : 代码段下方:
[UIView transitionWithView:self.view duration:.5 options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
self.myImageView.image = randomDog.image;
self.BreedLabel.text = randomDog.breed;
self.nameLabel.text = randomDog.name;
} completion:^(BOOL finished) {
}];
Because Inside the block self is being used and this block will hold on to self as its strong pointer. 因为在块内部正在使用self,并且这个块将保持self作为其强指针。
I read it somewhere that if one need to use self in side of the block its always good practice to create weekSelf like 我在某处读到,如果需要在块的一侧使用self,那么创建weekSelf总是很好的做法
id (nonatomic, weak) weakSelf;
weakSelf = self;
and then use the weakSelf inside the block. 然后在块内使用weakSelf。
Please help me if I understood the concept correctly. 如果我理解正确的概念,请帮助我。 Thanks in advance. 提前致谢。
You don't have retain cycle problem in this case. 在这种情况下,您没有保留周期问题。
You do have retain cycle, but it is temporary and won't cause any problem. 你确实有保留周期,但它是暂时的,不会造成任何问题。
The retain graph is like ( A -> B
means A retain B) 保留图就像( A -> B
表示A保留B)
self -> view -> animationBlock -> self
but after animation block is executed, it is destroyed and retain graph become 但是在执行动画块之后,它会被破坏并保留图形
self -> view
and it is all good 这一切都很好
Also the correct syntax to break retain cycle is 打破保留周期的正确语法也是
__weak __typeof__(self) weakSelf = self;
^ { // inside block
__typeof__(self) strongSelf = weakSelf;
// use strongSelf
}
or you can use @weakify
and @strongify
from libextobjc 或者您可以使用@weakify
和@strongify
从libextobjc
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.