3
在下面的代碼中,我是否正確理解保留週期問題,是否會出現保留週期?使用ARC調用self.managedObjectContext時調用perfromBlock時保留循環?
- (NSError *)importRoute:(NSDictionary *)route {
[self.importContext performBlockAndWait:^{
[NSEntityDescription insertNewObjectForEntityForName:@"Entity" inManagedObjectContext:self.importContext];
//do I get a retain cycle here?
}];
...
}
- (NSManagedObjectContext *)importContext {
if (!_importContext) {
id appDelegate = [[UIApplication sharedApplication] delegate];
_importContext = [[NSManagedObjectContext alloc] initWithConcurrencyType:NSPrivateQueueConcurrencyType];
_importContext.parentContext = [appDelegate managedObjectContext];
}
return _importContext;
}
大概是'self'擁有/保留了對'_importContext'的強引用,'_importContext'可能會複製傳入它的塊並且對它有很強的參考,並且該塊可能會保留'self',所以它肯定有保留週期的可能性。我並不認爲我能夠很好地掌握保留週期何時發生並且是一個問題,但我確實知道(1)叮噹有時會對我提出警告,並且(2)很容易產生弱點引用自我並在塊內部使用它,沒有缺點(至少,不在你的用例中,並不是我見過的)。 – Isaac 2013-03-23 04:48:33
[Objective-C/Blocks - 可能不是保留週期嗎?](http://stackoverflow.com/questions/15515093/objective-c-blocks-isnt-this-a-retain-cycle) – 2013-03-23 05:15:20
你是對的,自己擁有對_importContext的強有力的引用。編譯器不會在此處警告保留週期,儀器也不會顯示週期。我只想澄清一下,在這種情況下,實際上可能沒有一個循環,儘管在塊中捕獲了自我... – Ron 2013-03-23 05:17:01