I've got the following error:
2012-04-18 10:15:49.585 FoodXJournal[13931:fb03] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an NSManagedObjectModel for entity name 'CommodityTypes''
I've read most of the other questions with this subject, and done some checking.
I have a detail view controller containing a static table view. One cell of the table view is labeled "Delete" and is linked to a segue named "deleteCT". I want the the app to delete self.detailItem
and segue to the master view controller when this cell is tapped. Here's my method. All of the NSLog lines are for debugging this.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"deleteCT"]) {
if (![self.detailItem.myCommodities count]) {
NSLog(@"Testing");
NSLog(@"myCommodities is empty:%@", [self.detailItem.myCommodities count]);
NSLog(@"self.detailItem HAS a context:%@", [self.detailItem managedObjectContext]);
NSLog(@"self.detailItem is of CommodityTypes:%@", [[self.detailItem entity] name]);
NSLog(@"self.detailItem has a managed object model:%@", [[self.detailItem entity] managedObjectModel]);
[[self.detailItem managedObjectContext] deleteObject:self.detailItem];
}
}
}
Here's the log. I do get in to this method,
2012-04-18 10:15:49.544 FoodXJournal[13931:fb03] Testing
I meet my condition for deleting,
2012-04-18 10:15:49.544 FoodXJournal[13931:fb03] myCommodities is empty:(null)
The error message says '+entityForName: could not locate an NSManagedObjectModel for entity name 'CommodityTypes'
, but self.detailItem
HAS a context.
2012-04-18 10:15:49.545 FoodXJournal[13931:fb03] self.detailItem HAS a context:<NSManagedObjectContext: 0x6d7e740>
Yes, self.detailItem
is the entity type I'm thinking of:
2012-04-18 10:15:49.546 FoodXJournal[13931:fb03] self.detailItem is of CommodityTypes:CommodityTypes
And, yes, that entity type (NSLog(@"self.detailItem is of CommodityTypes:%@", [[self.detailItem entity] name]);
) has a managed object model:
The managed object model description is quite long, so I'm only showing the first part of it here:
2012-04-18 10:15:49.565 FoodXJournal[13931:fb03] self.detailItem has a managed object model:(<NSManagedObjectModel: 0x6d73250>) isEditable 0, entities {
Accounts = "(<NSEntityDescription: 0x6d71120>) name Accounts, managedObjectClassName Accounts, renamingIdentifier Accounts, isAbstract 0, superentity name Grandparent, properties {\n \"account_1\" = \"(<NSAttributeDescription: 0x6d6e9d0>), name account_1, isOptional 1, isTransient 0, entity Accounts,
scrolling down:
CommodityTypes = "(<NSEntityDescription: 0x6d71240>) name CommodityTypes, managedObjectClassName CommodityTypes, renamingIdentifier CommodityTypes, isAbstract 0, superentity name Grandparent, properties {\n myCommodities = \"(<NSRelationshipDescription: 0x6d701f0>), name myCommodities, isOptional 1, isTransient 0, entity CommodityTypes,
CommodityTypes
is defined in the managed object model.
So why does [[self.detailItem managedObjectContext] deleteObject:self.detailItem];
crash?!?
Is there some reason I can't delete self.detailItem
within prepareForSegue? Do I need to assign an action to the cell or label and then programatically call the segue?
Eureka! Thanks jrturton! It wasn't coming from a method that was creating a new managed object, but your idea did have me go back and use a bunch of break points just before and after that line. I narrowed it down to NSEntityDescription *entity = [NSEntityDescription entityForName:@"CommodityTypes" inManagedObjectContext:self.managedObjectContext];
in the master table view. On launch the app delegate sets up a managed object context and passes it to the master view. I forgot that when I'm segueing from the detail view to the NEXT INSTANCE of the master view I'm not necessarily going back to the original instance. I have to pass the managed object context to the next view controller.
- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"deleteCT"]) {
if (![self.detailItem.myCommodities count]) {
CommodityTypes * thisItem = self.detailItem;
NSManagedObjectContext * thisContext = [thisItem managedObjectContext];
FoodXCommodityTypesMasterViewController * nextView = [segue destinationViewController];
[thisContext deleteObject:thisItem];
nextView.managedObjectContext = thisContext;
}
}
}
This works now.