block下的weakSelf和strongSelf

PS:虽然确实很忙,但自己确实变懒了,又好久没有写文章啦~~~
下面记一篇开发过程中的小事,新来的人看了下面这段代码 又问我有了weakSelf 为什么还要加一个strongSelf。

    __weak __typeof(&*self)weakSelf = self;
    dispatch_async(dispatch_get_global_queue(0, 0), ^{
        __strong __typeof(weakSelf) strongSelf = weakSelf; 
        /**
         **/
    }); 

weakSelf只是弱引用,self对象被释放后,获取该对象也会被释放,它的属性会为空。
block内strongSelf对weakSelf做了一层强引用,避免weakSelf被释放掉。
也许这样的解释比较抽象。

下面跑几段代码来看看

    #define PBLog(prefix,Obj) {NSLog(@"%@::: 变量值:%p, 指向对象值:%@",prefix,Obj,Obj);}

    - (void)testForNOStrongSelf
    {
        MyTestForBlock *obj = [[MyTestForBlock alloc]init];
        obj.testText = @"TestForBlock";
        PBLog(@"obj", obj);
        __weak MyTestForBlock *weakObj = obj;
        PBLog(@"weakObj", weakObj);
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            PBLog(@"obj 释放前 weakObj", weakObj);
            sleep(2);
            
            NSLog(@"weakObj.testText::%@",weakObj.testText);
            PBLog(@"obj 释放后block内 weakObj", weakObj);
        });
        sleep(1);
        obj = nil;
        PBLog(@"obj 释放后 weakObj", weakObj);
    }

控制台输出:

    2017-08-21 18:41:00.866 TestForBlock[20598:453239] obj::: 变量值:0x608000016830, 指向对象值:
    2017-08-21 18:41:00.866 TestForBlock[20598:453239] weakObj::: 变量值:0x608000016830, 指向对象值:
    2017-08-21 18:41:00.866 TestForBlock[20598:453288] obj 释放前 weakObj::: 变量值:0x608000016830, 指向对象值:
    2017-08-21 18:41:01.866 TestForBlock[20598:453239] obj 释放后 weakObj::: 变量值:0x0, 指向对象值:(null)
    2017-08-21 18:41:02.868 TestForBlock[20598:453288] weakObj.testText::(null)
    2017-08-21 18:41:02.868 TestForBlock[20598:453288] obj 释放后block内 weakObj::: 变量值:0x0, 指向对象值:(null)

总结:obj类对象被释放之后,该对象的属性为空,相对应的弱应用对象也会为空,则该对象属性testText为空了,这时候如果若有对该属性进行行为操作,就有可能出现crash。

查看下面代码:

    - (void)testForStrongSelf
    {
        MyTestForBlock *obj = [[MyTestForBlock alloc]init];
        obj.testText = @"TestForBlock";
        PBLog(@"obj", obj);
        __weak MyTestForBlock *weakObj = obj;
        PBLog(@"weakObj", weakObj);
        dispatch_async(dispatch_get_global_queue(0, 0), ^{
            __strong MyTestForBlock *strongObj = weakObj;
            PBLog(@"obj 释放前 weakObj", weakObj);
            PBLog(@"obj 释放前 strongObj", strongObj);
            sleep(2);
            
            NSLog(@"weakObj.testText::%@",weakObj.testText);
            NSLog(@"strongObj.testText::%@",strongObj.testText);
    
            PBLog(@"obj 释放后block内 weakObj", weakObj);
            PBLog(@"obj 释放后block内 strongObj", strongObj);
        });
        sleep(1);
        obj = nil;
        PBLog(@"obj 释放后 weakObj", weakObj);
    }

控制台输出:

2017-08-21 18:39:43.847 TestForBlock[20570:451915] obj::: 变量值:0x608000002fe0, 指向对象值:
2017-08-21 18:39:43.847 TestForBlock[20570:451915] weakObj::: 变量值:0x608000002fe0, 指向对象值:
2017-08-21 18:39:43.847 TestForBlock[20570:451975] obj 释放前 weakObj::: 变量值:0x608000002fe0, 指向对象值:
2017-08-21 18:39:43.847 TestForBlock[20570:451975] obj 释放前 strongObj::: 变量值:0x608000002fe0, 指向对象值:
2017-08-21 18:39:44.848 TestForBlock[20570:451915] obj 释放后 weakObj::: 变量值:0x608000002fe0, 指向对象值:
2017-08-21 18:39:45.848 TestForBlock[20570:451975] weakObj.testText::TestForBlock
2017-08-21 18:39:45.848 TestForBlock[20570:451975] strongObj.testText::TestForBlock
2017-08-21 18:39:45.848 TestForBlock[20570:451975] obj 释放后block内 weakObj::: 变量值:0x608000002fe0, 指向对象值:
2017-08-21 18:39:45.849 TestForBlock[20570:451975] obj 释放后block内 strongObj::: 变量值:0x608000002fe0, 指向对象值:

总结:经过一次__strong 来强引用之后,即使在block过程中obj被释放, weakObj和strongObj也能在本次的block中存在,这样还是能获取到对象的属性。这样我们能确定strongSelf的作用是: 避免weakSelf被释放掉
为了避免在 block 的执行过程中,突然出现 self 被释放,导致出现若干异常!

你可能感兴趣的:(block下的weakSelf和strongSelf)