[英]Do I need a strongSelf in block to keep self alive and does the strongSelf really work?
This is what I have learned: when using self
retained block 这就是我学到的:使用self
保留块时
weakSelf
to break retain cycle 我需要一个weakSelf
打破保留周期 strongSelf
to prevent self
from becoming nil half-way 我需要一个strongSelf
self
,以防止self
在中途变成零 so I want to test if a strongSelf
can really keep self
alive like so: 所以我想测试一个strongSelf
是否真的可以使self
保持活力,如下所示:
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"viewDidLoad");
self.test = @"test";
NSLog(@"%@",self.test);
__weak typeof(self)weakSelf = self;
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
__strong typeof(weakSelf)strongSelf = weakSelf;
strongSelf.test = @"newTest";
NSLog(@"%@",strongSelf.test);
});
}
- (void)dealloc {
NSLog(@"dealloc");
}
@end
The ViewController will be pushed into a navigationController and pop out immediately. ViewController将被推入navigationController并立即弹出。 The output is 输出是
why the null? 为什么为空?
There is another question I have a project which contains tons of weakSelf
without strongSelf
in the block and I get plenty of signal 11 crash. 还有一个问题,我有一个项目,其中包含大量不包含weakSelf
strongSelf
,并且我收到大量11号崩溃的信号。 Is it related? 有关系吗? Is it worth to add strongSelf
to each of them? 是否strongSelf
为每个人都添加strongSelf
?
strongSelf
ensures that if self
was not released yet, it won't during the execution of the block. strongSelf
确保如果尚未释放self
,则不会在执行块期间释放它。
If the self is gone before you create strongSelf
, it will be nil. 如果在创建strongSelf
之前自我消失了,它将为零。
You should check, if strongSelf
does contain something: 您应该检查是否strongSelf
确实包含以下内容:
dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
typeof(weakSelf)strongSelf = weakSelf; // __strong is not needed
if(strongSelf){
strongSelf.test = @"newTest";
NSLog(@"%@",strongSelf.test);
}
});
The code in your block is executed after 3s delay. 块中的代码在延迟3秒后执行。 So by this time the weakSelf is already nil. 因此,到这个时候,weakSelf已经为零。 That's why strongSelf is nil 这就是为什么StrongSelf为零
I think you don't hit the problem right. 我认为您没有正确解决问题。 Using weakself will break the retain cycle, however this line: 使用弱势将打破保留周期,但是此行:
__strong typeof(weakSelf)strongSelf = weakSelf;
Does not capture strong reference to self until block is executed. 在执行块之前,不会捕获对self的强引用。 Hence if your pointer to VC outside of block is nil, VC will be released and weakSelf will be nil when you hit the block - meaning your strongSelf will be nil also. 因此,如果您指向该块之外的VC的指针为nil,则在您击中该块时,VC将被释放,weakSelf将为nil-这意味着您的strongSelf也将为nil。 Capturing strong reference guarantee only that if you have weak self at the begin of the block, you will have it until the end and it won't be dealloc meanwhile. 捕获强引用仅可确保如果您在块的开始处具有较弱的自我,则直到结束时您都将拥有它,并且同时不会取消分配。 However if weakSelf is nil at begin of the block - strongSelf will be nil also. 但是,如果在程序块开始时weakSelf为零,则strongSelf也将为零。 Moreover you do not need to declare it as strong inside block, as it will be strong by definition, hence it could be replaced by: 而且,您不需要将其声明为强内部块,因为根据定义它将是强内部块,因此可以将其替换为:
typeof(weakSelf)strongSelf = weakSelf;
or 要么
id strongSelf = weakSelf;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.