1
votes

Having an odd problem with Core Data...

I have a model where I set several other NSManagedObjects as part of another NSManagedObject (To One relations). It runs fine in the emulator but when I test on the device it crashes, giving me this error:

2014-08-10 03:14:13.292 Prologue[4099:60b] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '+entityForName: could not locate an entity named 'BookContents' in this model.' * First throw call stack: (0x184ad3100 0x1914b01fc 0x1847f2f84 0x1000794ec 0x10007846c 0x100068250 0x10006adf0 0x10006ae54 0x187ad10b0 0x187ad1044 0x187aba520 0x187ad0a44 0x187ad06d8 0x187acb370 0x187a9cb50 0x187a9ac40 0x184a937f4 0x184a92b50 0x184a90de8 0x1849d1dd0 0x18a671c0c 0x187b02fc4 0x100072fd4 0x100073014 0x191aa3aa0) libc++abi.dylib: terminating with uncaught exception of type NSException

BookContents is a model that has a relation to my main data model object Book. Here's the code that creates it:

func createBookModel() -> Book
{
    var book:Book = createFromEntity("Book") as Book;
    if (book != nil)
    {
        book.title = "New Book";
        book.bookContents = createFromEntity("BookContents") as BookContents;
        saveContext();
    }
    else
    {
        Log.error("Failed to create Book.");
    }
    return book;
}

func createFromEntity(entityName:String) -> NSManagedObject
{
    var obj:NSManagedObject = NSEntityDescription.insertNewObjectForEntityForName(entityName, inManagedObjectContext: moc) as NSManagedObject;
    return obj;
}

I'm not sure what is wrong here but since it runs on the emu but not on the device it could be that something else is wrong (maybe with the data model file?). However the entity naming is correct anywhere and I prefixed the entity classes in the data model with the project's module name.

6
Have you changed your model since you put the app on your device?bbarnhart

6 Answers

8
votes

We had problems with using project's module name prefixes for entity classes in the data model. Instead we have added @objc(ClassName) for generated classes for NSManagedObjects.

@objc(Book)
class Book: NSManagedObject {

    @NSManaged var ...

}

You might try to remove the prefixes and add @objc(ClassName) if it does not solve your problem.

3
votes

Make sure in your schema file in the Data Model inspector tab that the Class is set to {AppName}.Book.

1
votes

I figured out what was wrong: The datamodel file was corrupt. I've deleted the old one and recreated it from scratch and, hear hear, it works now. It must have happened when I set up a new project and copied over the files because I renamed the project in between. So copying over the data model file obviously screwed something up with it.

0
votes

You should set the class to {AppName}.Book. In the Data model in Schema file

0
votes

A different solution worked for me: I get this error when I create the NSManagedObject subclass as a Swift class, but not when I specify an Objective-C class (and include it in my bridging header).

0
votes

Check the spelling. My problem was that my entity name was Capitalized and I had loweredcased it. Those are hard to catch.