I want to add 10 elements to a Core Data store.
The problem is that only the last element is added.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Test" inManagedObjectContext:[self managedObjectContext]];
Test *test = [[[Test alloc] initWithEntity:entity insertIntoManagedObjectContext:[self managedObjectContext]] autorelease];
for (int i =0; i<10; i++)
{
test.text = @"Text";
test.index = [NSNumber numberWithInt:i];
[self saveContext];
}
You must insert
testinto the context inside of the loop, not before it:Also, I would save the context outside of the loop for performance reasons. You need only to save it once, not every time.