When should we change weakSelf to strongSelf?

Viewed 6044

I know that when we use block we should use weakSelf to avoid retain cycle.But I saw there is a strongSelf in block sometimes.

What confused me are:

  1. why must change weakSelf to strongSelf?
  2. if we don't change weakSelf to strongSelf what terrible thing would happen?
  3. when should we change weakSelf to strongSelf?

Hope someone can give an exact example.

Thanks in advance.

3 Answers

In any case you need the "self" after the callback return - it is never wrong to turn weak self to strong self, in worst case - just extra code. but the opposite is not true. here is a very simple example where you indeed need a strong ref.

 __weak typeof(self) weakSelf = self;
    [self.bgQueue1 addOperationWithBlock:^{
        [weakSelf doSomeMagic:weakSelf.importentArr arg:number];
        if (weakSelf.importentArr.count == 10){
            //we will have crash(or other unpredictable results) if weakSelf will turns to nil
            [DataBaseClass save:weakSelf.importentArr];
        }
    }];
Related