How to change NSManagedObjects: Name change causes error
While working on a project I ran into the following Issue, in my CoreData model I had an entity called Object, this worked fine in the simulator (with a warning), but not on an actual iOS device. SO I had to change the name of the Entity. First I tried just changing the name of the Object in my model, and generate a new NSManagedObject subclass based on that.
This gave me all sorts of errors so I decided to remove the entity en create an entirely new one. This object I called REObject (Real Estate Object), I generated a new subclass again, based on the new entity, en changed all my code to use REObject instead of object. I also cleaned my project, and deleted the app from my testing device, yet still I get errors, currently this is the one I cannot fix.
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'executeFetchRequest:error: A fetch request must have an entity.'
I use the following code to remove all the objects in case of receiving new Data and it throws the error when I execute the fetch:
- (void) deleteAllEntitiesOfType: (NSString *) entityType
{
NSManagedObjectContext *context 开发者_开发百科= [(Achmea_CatalogusAppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
NSFetchRequest * all = [[NSFetchRequest alloc] init];
[all setEntity:[NSEntityDescription entityForName:entityType inManagedObjectContext:context]];
[all setIncludesPropertyValues:NO]; //only fetch the managedObjectID
NSError * error = nil;
NSArray * objects = [context executeFetchRequest:all error:&error];
[all release];
//error handling goes here
for (NSManagedObject * o in objects) {
[context deleteObject:o];
}
[context save:&error];
}
This method was working fine before I changed the object name but now it keeps giving me this error.
Edit: It feels like the app/project does not see the changes I made to the datamodel.
This:
[NSEntityDescription entityForName:entityType inManagedObjectContext:context]
...is not returning a usable entity description for what ever reason.
I would suggest breaking that call out to it's own line and logging the return to see what your actually getting back.
All it takes is one typo anywhere from the data model to the method call to create this problem.
In the future, avoid names like object
and entity
. Objective-C has a global open name space so names like that can trigger naming collisions. Long, verbose names are the safest and , long term, easier to read.
精彩评论