问题
In ReactiveCocoa there is macro to prevent retain cycle @weakify
and @strongify. From my understanding @weakify
do something like what I usually do that is create __weak
reference for using in the block, but what about @strongify
?
Why do I need to make it strong again in the block?
Here is some sample usage:
@weakify(self);
[RACObserve(self, username) subscribeNext:^(NSString *username) {
@strongify(self);
[self validateUsername];
}];
回答1:
If you just use a weak reference within the block, self can get deallocated while the block is being executed. But if you want to ensure that self stays in memory until the block finishes execution, you have to convert the weak reference back to strong.
来源:https://stackoverflow.com/questions/28809163/what-is-the-reason-of-strongify