Oleksandr Matrosov
Oleksandr Matrosov

Reputation: 27133

Should I weakify self every time using block

Will this block cause a memory leak issue, because I am not using weakified sell:

[KNApi getCouponsWithSearchString:self.searchString withCouponsCount:self.coupons.count withSuccess:^(id object) {

    [self.coupons addObjectsFromArray:object[@"items"]];

    [self.hud hide:YES];
    [self.theTableView setHidden:NO];
    [self.theTableView reloadData];

} withFailure:^(id object) {
    [self hideLoadingIndicatorWithError:object];
}];

I know for example if we have [KNApi getCouponsWithSearchString... as a block property in self class, then it causes an issue.

So suppose that our stack will be destroyed and I will get a success invocation before that. Will it cause an issue with requesting itself?

Upvotes: 1

Views: 845

Answers (2)

Marcio Romero Patrnogic
Marcio Romero Patrnogic

Reputation: 1156

YES YOU SHOULD USE A WEAK SELF! Its a good practice

check this section, link below "Avoid Strong Reference Cycles when Capturing self"

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/WorkingwithBlocks/WorkingwithBlocks.html#//apple_ref/doc/uid/TP40011210-CH8-SW16

Upvotes: 0

Mercurial
Mercurial

Reputation: 2165

It depends. Does KNApi retain the block? Does self retain KNApi? What block does is it retains self strongly. It's no different than any other retain. You don't have to weakly retain self as long as you're sure what's happening with the block.

Upvotes: 1

Related Questions