NSFetchRequests returns 0 results when it should return 1
I am trying to perform a fetch on core data after i insert an entity, and I get 0 results. I have a Person Entity.And i built a fetch Request in the DataModule GUI with the predicate:
name == "PERSONNAME"
Just before searching for the name i insert and save it first. and i know that works because i display the names in a table and the coredata is saved after i close and open the application again.
Here is the code i coded for the fetch. Please help me understand why am i getting 0 results
-(Person *)findPersonByName:(NSString *)name{
NSLog(@"looking for person with name: %@",name);
NSDictionary *subs = [NSDictionary dictionaryWithObject:name forKey:@"PERSONNAME"];
NSAssert(self.managedObjectModel, @"anything wrong with managedObjectModel");//no
NSFetchRequest *fetch = [self.managedObjectModel fetchRequestFromTemplateWithName:@"getPersonByName" substitutionVariables:subs];
NSAssert(fetch, @"anything wrong with fetch?");//no
NSLog(@"fetch: %@",fetch);
NSError *error;
NSArray *result = [self.managedObjectContext executeFetchRequest:fetch error:&error];
NSLog(@"fetch request getQuestionsByParent: %u found (sub variables:%@, results:%@)", [result count], subs, result);
if (result == nil){
// Deal with error...
}
else if([result count]>0){
for (Person *person in result) {
开发者_开发知识库 NSLog(@"search result: %@",person.name);
}
}
return nil;
}
Please help resolve the problem. Thanks
In your predicate template:
name == "PERSONNAME"
should be:
name == $PERSONNAME
精彩评论