I am trying to fetch an entity from the core data depending on the attribute called “name”. Here is my code:
NSManagedObjectContext *context = [self managedObjectContext];
NSEntityDescription *vegetable = [NSEntityDescription insertNewObjectForEntityForName:@"Vegetable" inManagedObjectContext:context];
// get the vegetable using name
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSPredicate *predicate =
[NSPredicate predicateWithFormat:@"name == %@", myGardenViewModel.vegetable.name];
[request setEntity:vegetable];
[request setPredicate:predicate];
NSError *error;
NSArray *fetchedObjects = [self.managedObjectContext executeFetchRequest:request error:&error];
The last line throws the following exception:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Vegetable subentitiesByName]: unrecognized selector sent to instance 0x6bbcdd0'
any ideas?
The problem is the way you set up the
entityproperty of yourNSFetchRequest. WithinsertNewObjectForEntity:you are actually creating a new object to be inserted into the database later with[managedObjectContext save:&error];.What you want to do is to just set the entity for your fetch request, rather than inserting a new object: