【问题标题】:Should I weakify self every time using block我应该每次使用块来削弱自己吗
【发布时间】:2015-04-21 13:16:05
【问题描述】:

这个block会不会导致内存泄漏问题,因为我没有使用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];
}];

例如,我知道如果我们将 [KNApi getCouponsWithSearchString... 作为 self 类中的块属性,那么它会导致问题。

因此假设我们的堆栈将被销毁,并且在此之前我将获得成功的调用。它会导致请求自身出现问题吗?

【问题讨论】:

标签: ios ios7 ios5 objective-c-blocks


【解决方案1】:

这取决于。 KNApi 是否保留块?自己保留 KNApi 吗?块做的是它强烈地保留自我。它与任何其他保留没有什么不同。只要您确定块发生了什么,您就不必弱保留自我。

【讨论】:

  • 是的@JakubVano 评论也有帮助的link。所以在提供的代码中 KNApi 不保留块。自己也没有。
【解决方案2】:

是的,你应该使用弱自我!这是一个很好的做法

查看此部分,链接下方"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

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-10-07
  • 2016-07-06
  • 2010-10-24
  • 1970-01-01
  • 2020-12-27
  • 2017-08-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多