1

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?

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Matrosov Oleksandr
  • 25,505
  • 44
  • 151
  • 277
  • 1
    No issue there. Take a look for explanation http://stackoverflow.com/questions/20030873/always-pass-weak-reference-of-self-into-block-in-arc – Jakub Vano Apr 21 '15 at 13:32
  • @JakubVano, thanks man, I think I read about it before, but you link with those answers really helpful!!! – Matrosov Oleksandr Apr 21 '15 at 14:27

2 Answers2

1

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.

Mercurial
  • 2,095
  • 4
  • 19
  • 33
  • yep @JakubVano comment with helpful [link](http://stackoverflow.com/questions/20030873/always-pass-weak-reference-of-self-into-block-in-arc) as well. So in the code provided KNApi does not retain block. self also does not. – Matrosov Oleksandr Apr 21 '15 at 22:06
0

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

  • looks like you have right point of view, but it really depends, http://stackoverflow.com/questions/20030873/always-pass-weak-reference-of-self-into-block-in-arc – Matrosov Oleksandr Apr 21 '15 at 22:05