I am trying to write a simple table view editor for a Core Data entity. Unfortunately I'm running into problems.
The error occurs when adding the very first entity to the table. The process for bringing up the modal dialog is as follows:
NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Group" inManagedObjectContext:context];
insertNewObjectForEntityForName:[entity name] inManagedObjectContext:context];
NSManagedObject *newManagedObject = [[NSManagedObject alloc] initWithEntity:entity insertIntoManagedObjectContext:context];
NameEditController *dialog = [[NameEditController alloc] init];
dialog.managedObject = newManagedObject;
[newManagedObject release];
UINavigationController *navCtrlr = [[UINavigationController alloc] initWithRootViewController:dialog];
navCtrlr.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[[self navigationController] presentModalViewController: navCtrlr animated:YES];
[navCtrlr release];
Inside of NameEditController
, I have this after the Done button is pressed:
NSString* name = self.nameLabel.text;
[self.managedObject setValue:name forKey:@"name"];
NSError *error = nil;
if (![managedObject.managedObjectContext save:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
}
UIViewController *ctrl = [self parentViewController];
[ctrl dismissModalViewControllerAnimated:YES];
The very first time I create an object (when the list is empty) I get this:
Exception was caught during Core Data change processing: [ valueForUndefinedKey:]: the entity Group is not key value coding-compliant for the key "(null)".
If I fill out the 'name' field before bringing up the dialog, I am able to add the first entity successfully:
[newManagedObject setValue:@"New Group" forKey:@"name"]; // this works
I am using NSFetchedResultsController
to manage the table view BTW.
Thanks!