美文网首页iOS面试
iOS block内部为什么要加__strong

iOS block内部为什么要加__strong

作者: wp_Demo | 来源:发表于2018-12-27 16:23 被阅读0次
    block的循环引用这里就不说了,现在是有一种场景,比如在当前的VC中,延迟执行了block方法,但是当block执行的时候,当前的VC已经不存在了,获取不到到VC的属性了,我们该怎么解决

    定义一个block还有全局的字符串属性

    @property (nonatomic,copy)Block block;
    @property (nonatomic,strong)NSString *str;
    

    然后我们点击,vc返回了

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        __weak typeof(self) weakSelf = self;
        self.block = ^{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSLog(@"-----%@",weakSelf.str);
            });
        };
        self.block();
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    2018-12-27 15:42:47.773924+0800 TestDemo[33437:595246] dealloc
    2018-12-27 15:42:49.267220+0800 TestDemo[33437:595246] -----(null)
    

    可以从控制台看出,在block执行之前vc已经被销货了,所以再去向一个空的对象发送消息,肯定是发送失败的.
    那么我们为了保证block的正常执行,可以尝试使用在block内部使用: __strong,防止self提前释放

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        __weak typeof(self) weakSelf = self;
        self.block = ^{
            //在block的执行过程中,使用强对象对弱对象进行引用
            __strong TwoViewController *strongSelf = weakSelf;
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                NSLog(@"-----%@",strongSelf.str);
            });
        };
        self.block();
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    2018-12-27 15:45:20.427785+0800 TestDemo[33486:599182] -----123
    2018-12-27 15:45:20.427937+0800 TestDemo[33486:599182] dealloc
    

    可以发现,可以获取的到str属性,并且在打印完成之后VC才会被销毁
    那么问题来了,也就是本文的主题,为什么加了__strong就可以获取的到了呢?
    分析:既然获取的到,那就是没有销货,在iOS中控制一个视图是否被销货是由RetainCount来判断的,那么我们是否可以打印看下RetainCount来看下呢:
    首先百度了一个在ARC中获取RetainCount的方法:

    CFGetRetainCount((__bridge CFTypeRef)(weakSelf))
    

    我们先看下在没有加__strong的时候:

        printf("点击时 retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(self)));
        __weak typeof(self) weakSelf = self;
        self.block = ^{
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                //此处不可以打印,因为weakSelf已经被销货了,否则会崩溃
                printf("block 内部retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(weakSelf)));
                NSLog(@"-----%@",weakSelf.str);
            });
        };
        self.block();
        [self.navigationController popViewControllerAnimated:YES];
    
    点击时 retain count = 3
    dealloc时 retain count = 1
    2018-12-27 18:08:03.368435+0800 TestDemo[35790:805937] dealloc
    2018-12-27 18:08:05.059696+0800 TestDemo[35790:805937] -----(null)
    

    加了__strong:

    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
    {
        printf("点击时 retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(self)));
        __weak typeof(self) weakSelf = self;
        self.block = ^{
            //在block的执行过程中,使用强对象对弱对象进行引用
            printf("strongSelf赋值之前 retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(weakSelf)));
            __strong TwoViewController *strongSelf = weakSelf;
            printf("strongSelf赋值之后 retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(weakSelf)));
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2*NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                printf("block 内部retain count = %ld\n",CFGetRetainCount((__bridge CFTypeRef)(weakSelf)));
                NSLog(@"-----%@",strongSelf.str);
            });
        };
        self.block();
        [self.navigationController popViewControllerAnimated:YES];
    }
    
    点击时 retain count = 3
    strongSelf赋值之前 retain count = 4
    strongSelf赋值之后 retain count = 5
    block 内部retain count = 2
    2018-12-27 18:09:12.434576+0800 TestDemo[35823:808094] -----123
    dealloc时 retain count = 1
    2018-12-27 18:09:12.434785+0800 TestDemo[35823:808094] dealloc
    

    从以上我们可以得出结论 :

    在__strong修饰了weakSelf之后,VC的Retaincount增加了1,block代码块是存放在栈区的,执行完成之后,会被系统自动回收,这时候strongSelf会被释放掉,此时self的引用计数才恢复正常了.

    相关文章

      网友评论

        本文标题:iOS block内部为什么要加__strong

        本文链接:https://www.haomeiwen.com/subject/oaorlqtx.html