我应该什么时候在一个街区使用弱点,为什么在砌体中没有保留周期?

问题描述:

UIButton *testButton = [[UIButton alloc] init]; 
[self.view addSubview:testButton]; 
testButton.backgroundColor = [UIColor redColor]; 
[testButton mas_makeConstraints:^(MASConstraintMaker *make) { 
    make.width.equalTo(@100); 
    make.height.equalTo(@100); 
    make.left.equalTo(self.view.mas_left); 
    make.top.equalTo(self.view.mas_top); 
}]; 
[testButton bk_addEventHandler:^(id sender) { 
    [self dismissViewControllerAnimated:YES completion:nil]; 
} forControlEvents:UIControlEventTouchUpInside]; 

我在我的代码中使用了BlocksKit和Masonry。如果使用我BlocksKit,bk_addEventHandler,有一个保留周期,我认为这是因为自我保留self.view,保留testButton,保留自我。然而,当我单独使用Mansonry而没有BlocksKit,并且我在Masonry mas_makeConstraints中使用强烈自我时,发生在我身上的是没有保留循环,因为viewController可以调用dealloc方法。任何人都可以向我解释砌体中没有保留循环吗?我应该什么时候在一个街区使用弱点,为什么在砌体中没有保留周期?

这是因为该块套件块是保留用于执行在稍后的日期(从而创建,通提及自我,保留周期),而砌筑块执行更多或更少的权现在,然后扔掉。

同样,当您调用UIView方法时,您无需担心保留周期。这是因为一旦代码结束,运行循环结束,动画块被执行并丢弃。但是在NSNotification观察器块中引用self可能会导致保留周期,因为它只是被系统永久保存,直到您取消注册该通知,同时您保留观察者。

+0

进一步讨论:http://www.apeth.com/iOSBook/ch12.html#_unusual_memory_management_situations – matt