1

我在stackoverflow中尝试了很多解决方案,但我无法找到有效的解决方案。我有一个包含两个实体的核心数据模型:客户端和目标。两者都被NSManagedObject子类包装。在一对多关系中使用NSPredicate进行筛选

客户端有一些属性和称为目的地的一对多关系。 目的地有一个名为default_dest的属性,该属性由NSNumber和称为客户端的反向关系封装。

我有一个UITableViewController我在使用以下fetchedController属性。该请求运作良好。我能够检索存储在SQLite中的客户端。

if (fetchedResultsController) 
    return fetchedResultsController; 

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; 
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Client" inManagedObjectContext:self.managedObjectContext]; 
[fetchRequest setEntity:entity]; 

[fetchRequest setFetchBatchSize:20]; 

NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"code" ascending:YES]; 
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil]; 

[fetchRequest setSortDescriptors:sortDescriptors]; 

fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil]; 

fetchedResultsController.delegate = self; 

[fetchRequest release]; 
[sortDescriptor release]; 
[sortDescriptors release]; 

return fetchedResultsController; 

我会再来一步。我现在将过滤从先前请求中检索的目的地(包含在目的地NSSet中),供每个客户端使用。特别地,该目的地可以被添加仅当它的default_dest值为1

要解决本说明书中我试图添加一个NSPredicate类似如下:

NSPredicate* predicate = [NSpredicate predicateWithFormat:@"ANY destinations.default_dest == %@", [NSNumber numberWithInt:1]]; 

然后我将其设置在fetchRequest为:

[fetchRequest setPredicate:predicate]; 

每次运行请求时,它都会返回一个“to-many-relationship fault destinations ...”。这是什么意思?我看过iphone-core-data-relationship-fault但我不明白这是什么意思。

所以,我的问题是:是否有可能实现类似的目标?如果是的话,你有什么建议吗?

注意

很显然,我可以遍历设定目的地,但我不知道是否可能是一个昂贵的迭代和多少记录有。

回答

3

对于那些有兴趣

你需要说使用您的读取请求预取的关系

- (void)setRelationshipKeyPathsForPrefetching:(NSArray *)keys 

例如:

[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects: @"destinations", nil]]; 

在这个人核心数据预取您指定的关系并且不会触发故障。

此外,您可以通过限制其限制的结果数为您的读取请求:

- (void)setFetchLimit:(NSUInteger)limit 

希望它能帮助。

-1

这是在提取中检索特定值的另一种方法。也许这可以帮助(查找链接名称的文件):

Fetching Specific Values

相关问题