0

插入实体后,我试图对核心数据执行提取,结果为 0。我有一个人实体。我在 DataModule GUI 中使用谓词构建了一个获取请求:

名称 == "人名"

就在搜索名称之前,我先插入并保存它。我知道这是可行的,因为我在表格中显示了名称,并且在我关闭并再次打开应用程序后保存了核心数据。

这是我为获取而编写的代码。请帮助我理解为什么我得到 0 个结果

-(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;
}

请帮助解决问题。谢谢

4

1 回答 1

2

在您的谓词模板中:

name == "PERSONNAME"

应该:

name == $PERSONNAME
于 2010-02-16T16:50:44.980 回答