2012-06-12 100 views
0

這是Why do we have to set __block variable to nil?我應該在這裏設置__block變量爲零,爲什麼?

基本上都採用塊可引起ARC保留週期,我還是不明白,爲什麼

http://developer.apple.com/library/mac/#releasenotes/ObjectiveC/RN-TransitioningToARC/Introduction/Introduction.html#//apple_ref/doc/uid/TP40011226-CH1-SW11

-(void)getReviewAndView{ 
    if(![[GrabClass grab] cekInet]){return;} 
    CM(@"get review and view"); 
    [email protected]"0 view"; 
    [email protected]"0 review"; 

    Business * businessReviewed = [BNUtilitiesQuick currentBusiness]; 

    NSString *alamat=[NSString stringWithFormat:@"http://...",businessReviewed.ID]; 

    CLog(@"alamat:%@", alamat); 

    __block NSDictionary * dic = nil; 


    [Tools doBackground:^{ 
     dic=(NSDictionary *)[GrabClass JsonParser:alamat]; //dic get set here 
     [Tools doForeGround:^{ 
      NSString *countView= [[dic objectForKey:businessReviewed.ID] objectForKey:@"CountViews"]; 

      CLog(@"countView:%@", countView); 
      NSString *countReview=[[dic objectForKey:businessReviewed.ID] objectForKey:@"Review"]; //dic get used here 
      NSString * [email protected]"review"; 
      NSString * [email protected]"view"; 


      //blablabla 

      self.viewlbl.text=[NSString stringWithFormat:@"%@ %@",countView,view ]; 
      self.reviewlbl.text=[NSString stringWithFormat:@"%@ %@",countReview,reviews]; 
      dic =nil; //should this be called? What happen if it doesn't? 
     }]; 
    }]; 
} 

回答

1

如果你不知道,如果延續你有一個保留週期或沒有,你應該在儀器的「泄漏」工具下運行你的代碼,它會告訴你任何泄漏或保留週期。

另請注意,在最新版本的Xcode中,如果您創建這些父/塊週期中的一個,編譯器將爲您生成警告。

在你的情況下,你大概不需要擔心。 dic不保留引用它的塊,所以這裏沒有循環。但最好的檢查方法是使用泄漏/分配工具。

+0

啊我明白了。所以如果對象不包含對塊的引用,那麼就不存在真正的問題。謝謝。 –

相關問題