繁体   English   中英

在 ReactiveObjc 中使用 @strongify(self) 之前,我应该检查弱自我的存在吗?

[英]Should I check the existence of weakself before use @strongify(self) in ReactiveObjc?

作为weak和strong中的另一种做法,建议我们在加强之前先检查weakself是否为null。 它看起来像这样:

__weak weakself = self
someblock {
  if (weakself) {
    __strong self = weakself
    [self doSomeAction];
...
  }
}

我知道 rac 前面做了很多工作,但我想确定我们是否有必要检查它。 如果没有,@strongify(self) 如何做到这一点。 谢谢。

不,你应该在strongify之后strongify ,因为它可以停止存在,直到你加强它。

在加强之后它无论如何都不会停止存在,即使检查nil也不需要任何同步。 我不知道你从哪里得到这样无效的建议。

if (weakself) { // not nil here
    __strong self = weakself //already nil here
    [self doSomeAction]; //you don't have retain cycles,
    // but there is a potentical crash, for example if you are calling blocks

@strongify 完全按照您已经编写的方式进行操作,这只是一个方便的宏:

__strong typeof(weakSelf) self = weakself

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM