I'm wondering if someone could explain why the retain count of a newly created managed object is 2. Here is the code I'm working with:
NSManagedObjectContext *context = [self.fetchedResultsController man开发者_如何学JAVAagedObjectContext];
NSEntityDescription *entity = [[self.fetchedResultsController fetchRequest] entity];
Album *newAlbum = [NSEntityDescription insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
NSLog(@"Album retain count: %d", [newAlbum retainCount]);
According to the documentation for the insertNewObjectForEntity
method, it returns a newly created autoreleased object. So I would expect the retain count to be 1 instead of 2. Can someone explain?
Retain count is not guaranteed to be accurate, you may have encountered such a case.
I've made a sample project in which an object alloc/init was having a retain count of two, then 0 when released, etc...
Another reason that can explain this, is that if you have registered to get notifications of objects inserted in that NSManagedObjectContext, then the object is getting added to an NSSet that will be passed to you on that notification, hence the retain count of 2.
精彩评论