简体   繁体   English

为什么使用ivar会导致反应性可可中的保留周期?

[英]Why using an ivar can cause a retain cycle in reactive cocoa?

I use an ivar in a reactive cocoa's block, set weakify(self) and strongify(self) to break the retain cycle. 我在反应性可可块中使用了一个ivar,设置weakify(self)和strongify(self)来打破保留周期。 But when I run the code, It causes a memory leak, the controller doesn't call dealloc method. 但是,当我运行代码时,这会导致内存泄漏,控制器不会调用dealloc方法。 If I change the ivar to an property, it runs right. 如果我将ivar更改为属性,则它会正确运行。 I'm so confused about it and search for a long time. 我对此很困惑,并且搜索了很长时间。 please tell me why. 请告诉我为什么。

- (void)viewDidLoad {
     [super viewDidLoad];
     ...
     @weakify(self);
     [RACObserve(self, something) subscribeNext:^(id x) {
         @strongify(self);
         [_button setBackgroundColor:[UIColor redColor]];
     }];
}

- (void)dealloc {

}

@strongify(self)以名称self声明新变量,并在调用self.button时使用此新变量, _button隐式使用请勿覆盖self

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM