[英]No Retain Cycle, But why still got retain cycle warning?
我正在尝试使用 AFNetworking2.6.3 的 UIImageView 扩展来从远程服务器获取图像。 一切正常,图像已返回并成功渲染。 但我在 Xcode7.3.1 中收到一个保留周期警告:在此块中强烈捕获“单元格”可能会导致保留周期
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
if(self.dataSourceModel) {
Model *model = self.dataSourceModel.items[indexPath.row];
NSURL *url = [NSURL URLWithString:model.imageURL];
NSURLRequest *theRequest = [NSURLRequest requestWithURL:url];
UIImage *placeholderImage = [UIImage imageNamed:@"placeholder"];
//I don't use __weak cell in the block
[cell.imageView setImageWithURLRequest:theRequest placeholderImage:placeholderImage success:^(NSURLRequest * _Nonnull request, NSHTTPURLResponse * _Nullable response, UIImage * _Nonnull image) {
cell.imageView.image = image; //get warning at this line
[cell setNeedsLayout];
} failure:nil];
}
return cell;
}
我可以看到,因为单元实例已在块中捕获,因此该块具有单元实例的所有权(以及 cell.imageView)。 如果保留循环确实存在,则单元格或图像视图应该拥有块的所有权。
但我已经查看了UIImageView+AFNetworking.h和UIImageView+AFNetworking.m源代码,UIImageView 没有任何块属性。 块只是方法的参数,而不是实例变量。 UITableViewCell 也不拥有块的所有权。
我什至用Leaks检查过,Leaks没有报错。
所以我相信这里没有保留周期,但为什么我仍然在这里收到 Xcode 警告? 如果我使用 __weak cell __weak UITableViewCell *weakCell = cell;
,警告将消失。 但我还是想知道:
任何提示都会有所帮助,非常感谢。
当然没有保留循环,您可以通过省略完成块中的代码来避免警告。 看一下 AFNetworking imageView 子类(第 152 行), setImage...
的最终目的是设置图像视图的图像。 您的代码需要再次设置它,或者更改布局状态。
也不需要if (self.dataSourceModel)
。 如果数据源构建正确,我们已经回答了tableView:numberOfRowsInSection:
的行数,当模型为空时,该答案为零,完全避免调用cellForRow...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.