2

This question has something to do with the question I posted here: 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.

Community
  • 1
  • 1
kiyoshi
  • 837
  • 1
  • 8
  • 20

4 Answers4

10

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
  • 7,801
  • 6
  • 41
  • 46
  • I never would've thought about that..... fixed everything right up... :) Thanks a million! :D Though i have to admit, the new xCode 4 was really annoying when it came to changing the name... i had to create a new entity and copy all the attributes/connections... /: – Alex Zak Jun 29 '11 at 03:00
  • 5
    Thanks a million. I was stuck in the problem for the whole day. I used an entity "Account", ... always get the the error. I fixed by change it to "XXXAccount". – Cullen SUN Jan 26 '12 at 14:16
4

Look for a message like this from the debugger. It will confirm what Benjamin says.

Class MailMesssage is implemented in both /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator4.2.sdk/System/Library/PrivateFrameworks/Message.framework/Message and /Users/home/Library/Application Support/iPhone Simulator/4.2/Applications/FFFFFFFF-FFFF-0000-0000-AAAAAAAAAAA/Projects.app/Projects. One of the two will be used. Which one is undefined.

axel22
  • 32,045
  • 9
  • 125
  • 137
0

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
  • 1,043
  • 11
  • 27
0

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
  • 837
  • 1
  • 8
  • 20