Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

insertNewObjectForEntityForName returns wrong type

I've been pulling my hair for the last few hours trying to figure out why insertNewObjectForEntityForName is returning NSManagedObject even though I'm trying to insert an entity of my own type.

Things I have checked a thousand times:

  • Class name is set to the correct value in the diagram designer
  • Entity has many-to-many relationship with another entity, creating that other entity works as expected and I do get the properly typed output, I swear they are identical

What are the possible reasons why this could be happening? I don't know where to look from here, and google didn't help much :(

EDIT (RESOLVED):

This had to do with versioning... I removed the data model and recreated it again, and everything started working, before there were a few versions that were previously used... sigh sometimes asking a question gives an idea of how to find a solution

I'm still confused of why that was happening, even though it's solved now

like image 249
Nick Avatar asked Feb 27 '23 01:02

Nick


2 Answers

I ended up in a similar situation with a different solution. Through a series of unfortunate events (a namespace clash forced changing one of my entities) I ended up with a custom entity that had its Class set to the default of NSManagedObject in the data model rather than the proper custom subclass.

So before you go through the hassle of deleting your entire data model and rebuilding it (as I just did, thank god for git branching), just check to make sure your Class is still set properly.

like image 51
Dav Yaginuma Avatar answered Mar 05 '23 17:03

Dav Yaginuma


I had this happen as well but it wasn't a corrupt model it was actual a typo in my entity definition in the model file.

Basically the entities and attributes were all correct but the "ClassName" for my entity had a typo. This cause Core Data to have enough info to actually read and write the entity but it was not able to instantiate the class (since it didn't actually exist with the name I had mistyped)

like image 44
John K Avatar answered Mar 05 '23 16:03

John K