NSFetchRequests 在应该返回 1 的时候返回 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在您的谓词模板中:
应该是:
In your predicate template:
should be: