views:

436

answers:

2

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!

A: 

In your first code block, the third line of code appears to be out of context. Not sure if there's something there that could be contributing.

Secondly, the easiest way to get yourself an NSManagedObject into a NSManagedObjectContext from an entity name is to use the [NSEntityDescription insertNewObjectForEntityName:inManagedObjectContext:] selector.

So, I'd do something like:

NSManagedObjectContext *context = [fetchedResultsController managedObjectContext];
NSManagedObject *newObject = [NSEntityDescription insertNewObjectForEntityName:@"Group" inManagedObjectContext:context];

You won't need to release newObject anymore, since the [NSEntityDescription insertNewObjectForEntityName:inManagedObjectContext:] selector will return an object with a retain count of 0. Also, make sure that you have NameEditController specifying its managedObject property as retained.

To address your actual issue, it sounds like maybe you have 'name' specified as a required property in your data model? A screenshot showing the details for 'name' in your data model would help.

refulgentis
A: 

Yarr... sorry guys, it was actually in my didChangeObject:atIndexPath:forChangeType:newIndexPath: function hastily copied from elsewhere. Apparently an exception thrown here can get obscured inside of the save: method.

sehugg
(also .. NSZombieEnabled is nice .. http://developer.apple.com/mac/library/technotes/tn2004/tn2124.html)
sehugg