2012-07-03 31 views
1

我有以下代碼下載一個圖像:ASIHTTPRequest completionBlock + ARC

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]; 

而且雖然我使用的是__block typeof (self)標識傳遞自成塊,它仍然被保留。我也嘗試__weak MyClassName *bself = self;,它仍然進入保留週期。看起來我在這裏錯過了一些東西,任何人都可以讓我知道我究竟做錯了什麼?

僅供參考imageRequest是我的.m文件類別中的__strong iVar。

在此先感謝。

+0

你設置imageRequest強? @propert(非原子,強)ASIHTTPRequest * imageRequest; – janusbalatbat

+0

是的,iVar在默認情況下是強壯的,但我使用另一個請求作爲強壯的一個弱引用,將其保存在內存中用於進一步操作,如取消。 – Eugene

回答

1

嘗試

__block __unsafe_unretained typeof (self) bself = self; 

- 編輯 -

accesing高德時,實際上是解決問題

註釋,使用bself.property做到這一點。如果你直接訪問你的ivars,它會得到一個保留週期。

+0

編譯器報告錯誤:「類型'typeof(self)'(aka'MyClass * const __strong')已經設置了保留屬性」 – Eugene

+0

使用MyClassName更改typeof(self)* – javieralog

+0

按您的建議更改。班級仍然進入保留週期。還有其他建議嗎? – Eugene