views:

300

answers:

3

This question has something to do with the question I posted here: http://stackoverflow.com/questions/1230858/iphone-core-data-crashing-on-save however the error is different so I am making a new question. Now I get this error when trying to insert new objects into my managedObjectContext:

 *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', 
 reason: '"MailMessage" is not a subclass of NSManagedObject.'

But clearly it is:

@interface MailMessage : NSManagedObject { ....

And when I run this code:

 NSManagedObjectModel *managedObjectModel = [[self.managedObjectContext
    persistentStoreCoordinator] managedObjectModel];

 NSEntityDescription *entity =[[managedObjectModel entitiesByName] 
    objectForKey:@"MailMessage"];

 NSManagedObject *newObject = [[NSManagedObject alloc] initWithEntity:entity 
    insertIntoManagedObjectContext:self.managedObjectContext];

It runs fine when I do not present an MFMailComposeViewController, but if I run this code in the

- (void)mailComposeController:(MFMailComposeViewController*)controller 
  didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {

method, it throws the above error when creating the newObject variable. The entity object when I use print object produces the following:

(<NSEntityDescription: 0x1202e0>) name MailMessage, managedObjectClassName MailMessage, 
   renamingIdentifier MailMessage, isAbstract 0, superentity name (null), properties {

in both cases, so I don't think the managedObjectContext is completely invalid. I have no idea why it would say MailMessage is not a subclass of NSManagedObject at that point, and not at the other.

Any help would be appreciated, thanks in advance.

A: 

Try resetting the Simulator or uninstalling the application from your device. Often the NSInternalInconsistencyException has to do with problems with changing the datamodel and the database not being updated accordingly.

Matthew Bischoff
I've tried that :( still same error.
kiyoshi
A: 

I was able to workaround this by creating the MailMessage object before presenting the modal view controller. Once the MailMessage object was already created, saving changes did not present a problem. A strange workaround and not addressing the actual problem as far as I know, but it works.

kiyoshi
A: 

Class MailMessage could be implemented in a library or somewhere else in the framework. As Objective C does not implement namespaces, one of the two will be used. Which one is undefined. Try giving your class a different name to quickly resolve the issue.

Benjamin Ortuzar