【发布时间】:2014-09-10 16:02:27
【问题描述】:
`我承认我不是 ARC 方面的专家,但通过一些研究和一些很棒的文章(如 this),我相信我掌握了基本知识。
但是,我目前很难过。我有一个属性定义如下。
@property (nonatomic,retain) Foo *foo;
在我的init 中,我执行以下操作。
if(self = [super init]) {
_foo = [[Foo alloc] initWithDelegate:self];
// async the rest
__weak typeof(self) weakSelf = self;
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT,
(unsigned long)NULL), ^(void) {
__strong typeof(weakSelf) strongSelf = weakSelf;
if (strongSelf.foo != nil) {
[strongSelf.foo runTests];
}
});
}
return self;
}
这是我的dealloc
- (void)dealloc {
_foo = nil;
}
如果 dispatch_aync 块被注释掉,我看到我的 Foo dealloc 在 foo 设置为 nil 后立即被调用。注释块后,不会调用 foo 的 delloc。
我的保留周期正确吗?知道怎么做吗?
【问题讨论】:
-
顺便说一句,我建议在
Foo属性的定义中使用strong而不是retain。在这种情况下几乎没有实际影响,但在 ARC 代码中,您应该使用strong和weak,而不是retain和assign。此外,在 ARC 代码中,您不再需要手动nil实例变量,因此您可以停用dealloc方法。
标签: ios objective-c automatic-ref-counting retain-cycle