I am struggling with Core data. I get the infamous error “Failed to call designated initializer on NSManagedObject class”. For a newbie who reads the documentation, started from a standard template in Xcode 4.2, existence/occurence of this error doesn’t make any sense.
How could there possibly be a failure in initialization of a managed object?
I did not modify my custom generated classes from model in any way. That means, I did not override that default initializer (initWithEntity:insertIntoManagedObjectContext:)of those NSManagedObject subclasses. Why would it not be succesfully called?
My whole setup is very basic.
I have one context, one store coordinator, and one model. This was setup up by the template.
I edited the Data Model visually, I have four entities there. Then I generated four classes with dynamic properties.
Now the only thing I am doing is passing a reference to the context from AppDelegate to my custom ViewConttroller. In this custom ViewController, I try to add a new entity into the Core Data context.
Is there some methodology or a set of steps with which I can test, whether the whole Core data stack was correctly initialized? Whether the store exists, whether the context is ok?
NSManagedObjectModel *managedObjectModel = [[managedContext persistentStoreCoordinator] managedObjectModel];
NSEntityDescription *entity = [[managedObjectModel entitiesByName] objectForKey:@"ComplexLocation"];
currentComplexLocation = [[ComplexLocation alloc] initWithEntity:entity insertIntoManagedObjectContext:managedContext];
The problem that the error complains about is that you didn’t call the designated initializer. If you’ve subclassed NSManagedObject, you need to make sure that any initializers call that method. If you’re instantiating NSManagedObject itself, you need to do it by calling that method.
Usually, the easy way to add an object to your context is to call
+[NSEntityDescription insertNewObjectForEntityForName:inManagedObjectContext:].I think you mean a new managed object. It’s unusual to add an entity at run time. In any case, you should show the code where you do that.