Is any probability that a test object will be deallocated before [self saveContext] ?
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Test" inManagedObjectContext:[self managedObjectContext]];
for (int i = 0; i < 10; i++)
{
Test *test = [[[Test alloc] initWithEntity:entity insertIntoManagedObjectContext:[self managedObjectContext]] autorelease];
test.text = @"Text";
test.index = [NSNumber numberWithInt:i];
}
[self saveContext];
No, unless under a couple of specific circumstances, and lets figure out why. Assuming this is on the main thread, before your selector is called, then the system will create a
NSAutoreleasePoolfor you, before your function and then after.So, if when expanded, your code looks like this:
entitywould be released immediately after your function exits, which is after[self saveContext]. ARC would solve a lot of these problems, if you chose to enable it.Warning!
Please note that this is not the actual code apple uses, the pool is only drained every frame, not every method, but when memory runs thin, the autorelease pool will automatically drain, so, IF your device is running out of memory, then theoretically, this could cause entity to be released early, but if that happens, you have other problems to worry about.
Also note that when dealing with threads, you must create your own autorelease pool, the system will NOT do that for you. That is not what you appear to be doing here, but if the situation does occur, remember to wrap your selector in an
@autoreleaseblock.