Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

should I check the existance of weakself before @strongify(self) ? #172

Open
yhvh009 opened this issue Mar 12, 2020 · 3 comments
Open

should I check the existance of weakself before @strongify(self) ? #172

yhvh009 opened this issue Mar 12, 2020 · 3 comments

Comments

@yhvh009
Copy link

yhvh009 commented Mar 12, 2020

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 is it necessary that we should check it or not. And if not, how does the @strongify(self) do this. Thanks.

@ikesyo
Copy link
Member

ikesyo commented Mar 12, 2020

Looks like you are referring to the 2.x ReactiveCocoa Objective-C API. I transfer this to https://github.com/ReactiveCocoa/ReactiveObjC.

@ikesyo ikesyo transferred this issue from ReactiveCocoa/ReactiveCocoa Mar 12, 2020
@n8gray
Copy link

n8gray commented Mar 17, 2020

Generally you want to do the opposite, since weakself can theoretically become null between the time you check and the time you strongify it.

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

Generally you want to do the opposite, since weakself can theoretically become null between the time you check and the time you strongify it.

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

old conversation here:) but why we would need to check self for nil in that case? if calling on nil method will do nothing anyway.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants