简体   繁体   中英

No Retain Cycle, But why still got retain cycle warning?

I am trying to use UIImageView extension of AFNetworking2.6.3 to get images from a remote server. everything works fine, images have returned and rendered successfully. but I get a retain cycle warning in Xcode7.3.1: Capturing 'cell' strongly in this block is likely to lead to a retain cycle

-(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;
}

I can see since the cell instance has been captured in the block, so the block has the ownership of the cell instance(and cell.imageView as well). if the retain cycle really exists, the cell or imageView should have the ownership of the block.

but I have reviewed the UIImageView+AFNetworking.h and UIImageView+AFNetworking.m source code, the UIImageView doesn't have any property of blocks. the block is just a parameter of the method, not a instance variable. UITableViewCell doesn't have the ownership of the blocks either.

I even used Leaks to check, and Leaks didn't report any error.

So I believe there is no retain cycle here, but why I still get the Xcode warning here ?? If I use __weak cell __weak UITableViewCell *weakCell = cell; , the warning will go away. But I still wanna know:

  • does it have a retain cycle here?
  • do I really need to use __weak cell?
  • or maybe imageView or cell really has the ownership of the block which I didn't realize?

Any hints will help, thanks a lot.

There's certainly no retain cycle, and you can avoid the warning by omitting the code in the completion block. Taking a look at the AFNetworking imageView subclass (line 152), the culminating point of setImage... is to set the image view's image. There's need for your code to set it again, or to change the layout state.

There should also be no need for if (self.dataSourceModel) . If the datasource is built right, we've answered the number of rows in tableView:numberOfRowsInSection: , and that answer is zero when the model is empty, avoiding the call to cellForRow... altogether.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM