How do you insert multiple/sequential new rows in coredata and xcode?
-(void)LoadDB{
CoreDataAppDelegate *appdelegate = [[UIApplication sharedApplication]delegate];
context = [appdelegate managedObjectContext];
NSManagedObject *newPref;
newPref = [NSEntityDescription
insertNewObjectForEntityForName:NSStringFromClass([Preference class])
inManagedObjectContext:context];
NSError *error;
[newPref setValue: @"0" forKey:@"pid"];
[context save:&error];
[newPref setValue: @"1" forKey:@"pid"];
[context save:&error];
}
The code above just over writes the previous entry. What is the correct procedure to insert the next row?
You need a new insert statement for each core data managed object. Otherwise you are only editing the existing MO. Also, you only need to save once at the end.
-(void)LoadDB{
CoreDataAppDelegate *appdelegate = [[UIApplication sharedApplication]delegate];
context = [appdelegate managedObjectContext];
NSManagedObject *newPref;
NSError *error;
newPref = [NSEntityDescription
insertNewObjectForEntityForName:NSStringFromClass([Preference class])
inManagedObjectContext:context];
[newPref setValue: @"0" forKey:@"pid"];
newPref = [NSEntityDescription
insertNewObjectForEntityForName:NSStringFromClass([Preference class])
inManagedObjectContext:context];
[newPref setValue: @"1" forKey:@"pid"];
// only save once at the end.
[context save:&error];
}