美文网首页
weakSelf与strongSelf

weakSelf与strongSelf

作者: 小羊爱学习 | 来源:发表于2019-11-11 11:21 被阅读0次

1、在使用block时,如果block内部需要访问self的方法、属性、或者实例变量应当使用weakSelf

    __weak __typeof__(self) weakSelf = self;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        weakSelf.titleLabel.text = @"111"; 

         weakSelf.nameLabel.text = @"222"; 

    });
`

2、如果在block内需要多次访问self,则需要使用strongSelf

    __weak __typeof__(self) weakSelf = self;

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        __strong __typeof(self) strongSelf = weakSelf;

        [strongSelf doSomething];

        [strongSelf doOtherThing];

    });

3、系统的block内部不需要使用weakSelf和strongSelf。

4、为了防止weakSelf 有可能被释放,通常和strongSelf一块上场。

__weak typeof(self) weakSelf = self;
[self doSomeBackgroundJob:^{
    __strong typeof(weakSelf) strongSelf = weakSelf;
    if (strongSelf) {
        ...
    }
}];

5、多层嵌套block。

    __weak typeof(self) weakSelf = self;
    [self.device connect:10 success:^{
        __strong typeof(self) strongSelf = weakSelf;
        __weak typeof(self) weakSelf2 = strongSelf;
        [strongSelf.device setBoMoDeviceWifi:jsonString timeout:10 callback:^(CMDAckType ack) {
            __strong typeof(self) strongSelf2 = weakSelf2;
            [strongSelf2.device disconnect];
            [strongSelf2 removeLoadingView];

            if (ack == CMDAckTypeSuccess) {
                [strongSelf2 showToastWithText:LOCALIZATION(@"setDeviceWifi Success!")];
            }else{
                [strongSelf2 showToastWithText:LOCALIZATION(@"setDeviceWifi Failed!")];
            }
        }];
        NSLog(@"-----");//因为是strongSelf强指针指着第二个block,所以即使走到这里,第一个代码块也不会释放。只有等第二个结束才会一起释放。

    } failed:^(CMDAckType ack) {
        __strong typeof(weakSelf) strongSelf = weakSelf;
        [strongSelf removeLoadingView];
        [strongSelf showToastWithText:LOCALIZATION(@"Connect Failed!")];
    }];
//weakSelf是为了避免循环引用,strongSelf是为了防止weakSelf被提前释放。如此类推,嵌套的第二个block也应该这样做。不管多少个都这样。

相关文章

  • RN 调 js 核心代码

    ^{RCTJSCExecutor *strongSelf = weakSelf;if (!strongSelf |...

  • 不一样的书写样式

    **********************1.block中weakSelf 与 strongSelf******...

  • 宏定义

    1.weakSelf和strongSelf

  • weakSelf与strongSelf

    1、在使用block时,如果block内部需要访问self的方法、属性、或者实例变量应当使用weakSelf 2、...

  • StrongSelf

    weakSelf : 防止循环引用 strongSelf: 防止释放 需要 强引用weakSelf,主要是处理一...

  • weakSelf/strongSelf

    1.Retain Circle的由来 当A对象里面强引用了B对象,B对象又强引用了A对象,这样两者的retainC...

  • weakSelf & strongSelf

    循环引用 循环引用不做过多的解释,两个对象互相持有对方,谁都无法先被释放掉。循环引用经常是由于使用block而引起...

  • weakSelf strongSelf

    解决 retain circle Apple 官方的建议是,传进 Block 之前,把 ‘self’ 转换成 we...

  • iOS宏定义

    1 weakself和strongself #ifndef weakify #if DEBUG #ifhas_fe...

  • weakSelf 或 strongSelf

    在block中为了防止循环引用会使用weakSelf 或者 strongSelf那么什么时候使用weakSelf,...

网友评论

      本文标题:weakSelf与strongSelf

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