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

270 Views Asked by At

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.

1

There are 1 best solutions below

0
Cy-4AH On

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