我应该将__block变量设置为nil,为什么?

时间:2012-06-12 15:47:01

标签: objective-c automatic-ref-counting

这是Why do we have to set __block variable to nil?

的延续

基本上使用块会导致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");
    self.viewlbl.text=@"0 view";
    self.reviewlbl.text=@"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 * reviews=@"review";
            NSString * view=@"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 个答案:

答案 0 :(得分:1)

如果你不知道你是否有保留周期,你应该在Instruments中的“Leaks”工具下运行你的代码,它会告诉你任何泄漏或保留周期。

另请注意,在最新版本的Xcode中,如果您创建其中一个父/块循环,编译器将为您生成警告。

在您的情况下,您可能无需担心。 dic不保留引用它的块,因此这里没有循环。但最好的检查方法是使用Leaks / Allocations仪器。