作为弱者和强者的另一种做法,建议我们在加强弱者之前先检查其自身是否为空。看起来像这样:
__weak weakself = self
someblock {
if (weakself) {
__strong self = weakself
[self doSomeAction];
...
}
}
我知道rac要做很多工作,但是我想确定是否有必要我们进行检查。如果没有,@ strongify(self)如何做到这一点。谢谢。
不,您应该在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