I've been using Core Data since it was released for iOS, but I'm having trouble figuring out what's going on with an insertNewObjectForEntityForName method invocation for one of my entities. When the method is called it simply returns a nil object and does not throw an exception.
Here's the code:
@try {
self.observation = [NSEntityDescription insertNewObjectForEntityForName:@"Observation" inManagedObjectContext:moc];
NSLog(@"No exception on insert");
}
@catch (NSException *exception) {
NSLog(@"Insert exception - %@", exception.description);
}
NSLog(@"observation: %@", self.observation ? self.observation.description : @"nil");
and here's the output:
2012-10-19 10:16:08.749 Track[63779:c07] No exception on insert
2012-10-19 10:16:08.750 Track[63779:c07] observation: nil
I can perform the insert on all the other entities in my ERD. It's just this one that's mocking (pun intended) me.
(Xcode 7.2 Swift project) After renaming an entity, my data model got confused and began returning nil objects when performing insertNewObjectForEntityForName
. Here are the three things I did to fix this:
I first used @PhillipMills' suggestion:po mainQueueContext.persistentStoreCoordinator?.managedObjectModel
and in the output I saw that the "managedObjectClassName" was the old entity/class name with the project prefixed.
... NewEntityName = "(< NSEntityDescription: 0x7f9773783440>) name DetectedData, managedObjectClassName MyProject.OldEntityName, ...
To try to resolve this, I deleted and recreated the entity. But this also returned nil objects when inserting. I looked in interface builder and saw that the Class field was blank. I set the class name to resolve this.
missing objc() declaration:
import Foundation
import CoreData
class NewEntityName: NSManagedObject {
// Insert code here to add functionality to your managed object subclass
}
Fixed Version:
import Foundation
import CoreData
@objc(NewEntityName) //THIS WAS MISSING
class NewEntityName: NSManagedObject {
// Insert code here to add functionality to your managed object subclass
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With