Search code examples
iphoneobjective-ciosasihttprequestblock

ASIHTTPRequest completionBlock + ARC


I have the following code to download an image:

  imageRequest = [ASIHTTPRequest requestWithURL:[NSURL URLWithString:magazineItem.contentURL]];
  __weak ASIHTTPRequest *weakRequest = imageRequest;

  __block typeof (self) bself = self;
  [imageRequest setCompletionBlock:^{
    if (weakRequest.responseStatusCode == 200) {
      bself.imageData = weakRequest.responseData;
      [[DataAccessLayer sharedInstance] storeTemporaryContentData:bself.imageData url:magazineItem.contentURL];
      bself.contentImage = [UIImage imageWithData:bself.imageData];
      if (bself.contentImage != nil) {
        if (bself.magazineItem.presentationStyle.intValue != -1) {
          [bself setPresentationStyle:bself.magazineItem.presentationStyle.intValue];
        }
        else {
          [bself setPresentationStyleForImage:bself.contentImage];
        }
      }
      else
        [bself.delegate contentItemViewUnavailable:bself];
    }
    else {
      [bself.delegate contentItemViewUnavailable:bself];
    }
  }];

  [imageRequest setFailedBlock:^{
    if (weakRequest.error.code == 4)
      return;
    [bself.delegate contentItemViewUnavailable:bself];
  }];

  [imageRequest startAsynchronous];

And though I'm using a __block typeof (self) identifier to pass the self into the block, it still gets retained. I also have tried __weak MyClassName *bself = self; and it still gets into retain cycle. It seem I'm missing something here, can anyone fill me up with what exactly am I doing wrong?

For reference imageRequest is a __strong iVar in my .m file category.

Thanks in advance.


Solution

  • try

    __block __unsafe_unretained typeof (self) bself = self;
    

    -- EDIT --

    The comment that actually solved the issue

    when accesing ivars, do it using bself.property. If you access your ivars directly it will get a retain cycle.