0

As the other practice in weak and strong, it recommends that we should check whether the weakself is null before we strongify it. It looks like this:

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

I know rac does a lot of work ahead, but I want to make sure whether it's necessary that we should check it or not. And if not, how does the @strongify(self) do this. Thanks.

jayess
  • 49
  • 6
richar lee
  • 11
  • 1
  • In general, there is little need to check for `nil` in Objective-C except some specific situations. Even if `self` is `nil`, your code will work correctly without the check. – Sulthan Mar 16 '20 at 13:20

1 Answers1

1

No, you should check after strongify, because it can stop existing till you strongify it.

After strongify it won't stop existing anyway, even check for nil will not required any synchronizations. I don't know where you did get such invalid recommendations.

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 do it exactly in such way how you already write, it's just a convenience macro:

__strong typeof(weakSelf) self = weakself
Sulthan
  • 128,090
  • 22
  • 218
  • 270
Cy-4AH
  • 4,370
  • 2
  • 15
  • 22