I am reading the book, it says: To break the strong reference cycle, you declare a __weak pointer outside the block that points to self. Then you can use this pointer inside the block instead of self:
__weak BNREmployee *weakSelf = self; // a weak reference
myBlock = ^{
NSLog(@"Employee: %@", weakSelf);
};
The block’s reference to the BNREmployee instance is now a weak one, and the strong reference cycle is broken. However, because the reference is weak, the object that self points to could be deallocated while the block is executing. You can eliminate this risk by creating a strong local reference to self inside the block:
__weak BNREmployee *weakSelf = self; // a weak reference
myBlock = ^{
BNREmployee *innerSelf = weakSelf; // a block-local strong reference
NSLog(@"Employee: %@", innerSelf);
};
Why I cannot simply do the following?
myBlock = ^{
BNREmployee *innerSelf = self; // a block-local strong reference
NSLog(@"Employee: %@", innerSelf);
};
or
BNREmployee *strongSelf = self;
myBlock = ^{
BNREmployee *innerSelf = strongSelf; // a block-local strong reference
NSLog(@"Employee: %@", innerSelf);
};