NSFetchRequests 在应该返回 1 的时候返回 0 结果

发布于 2024-08-21 22:40:25 字数 1236 浏览 5 评论 0原文

我尝试在插入实体后对核心数据执行提取,但得到 0 个结果。 我有一个 Person 实体。我在 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;
}

请帮助解决问题。 谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

七婞 2024-08-28 22:40:25

在您的谓词模板中:

name == "PERSONNAME"

应该是:

name == $PERSONNAME

In your predicate template:

name == "PERSONNAME"

should be:

name == $PERSONNAME
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文