简体   繁体   English

我需要强壮自我来保持自我活力,强壮自我真的有效吗?

[英]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保留块时

  1. I need a weakSelf to break retain cycle 我需要一个weakSelf打破保留周期
  2. I need a 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.

相关问题 我是否需要在代码块中检查strongSelf = weakSelf分配是否为零? - Do I need to check for nil on my strongSelf = weakSelf assignment inside a block? 在从 UIViewController 调用的非保留完成中引用 self 时,weakSelf/strongSelf 舞蹈真的有必要吗? - Is the weakSelf/strongSelf dance really necessary when referencing self inside a non-retained completion called from a UIViewController? 当weakSelf在块中为零时,应该添加strongSelf - when weakSelf will be nil in block ,when should add strongSelf 弱自我(好),强自我(坏)和阻止(丑陋) - weakSelf (the good), strongSelf (the bad) and blocks (the ugly) 如果从Block调用的方法使用self,是否需要使用弱self指针? - Do I need to use a weak self pointer if a method called from a Block uses self? 我真的需要自定义UIView中的drawRect()吗? - Do I really need drawRect() in custom UIView? 我真的需要导航控制器吗? - Do I really need a navigation controller? 如何像UITabBar应用程序一样使视图保持活动状态? - How do I keep views alive like UITabBar applications? 使iOS App保持活动状态以使用MQTT协议 - Keep iOS App alive to work with MQTT protocol 如何让一段代码在后台保持活动状态? - How to keep a block of code alive in background?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM