Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the reason of @strongify

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];
}];
like image 951
sarunw Avatar asked Mar 02 '15 11:03

sarunw


1 Answers

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.

like image 91
rounak Avatar answered Sep 19 '22 00:09

rounak