views:

99

answers:

1

Hi, 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

+1  A: 

In your predicate template:

name == "PERSONNAME"

should be:

name == $PERSONNAME
Rob Napier
when i create the predicate in the xcdatamodel with :name == "$PERSONNAME"its still dosent work, I cant find the way to loose the "" around the PERSONNAME
Cocoa student
I did now realize that i needed to edit it in the "expression mode" after i created the predicate. So what i did was copy the predicate and then edit again and select expression from the dropdown then pasted and changed to :name == $PERSONNAMEThanks Rob.
Cocoa student