【发布时间】:2012-09-04 17:02:30
【问题描述】:
在我的 CoreData 模型中有 3 个实体
与 Sections 实体有 section 关系的 Formation 和与 Chapters 实体有 Chapters 关系。
所以一个编队包含 1 或 n 个包含 1 或 n 章的部分
在我的实体部分中,有一个sortNB 属性(一种id),所以我想在我的请求中对我的部分进行排序。
我尝试过:
NSFetchRequest *request = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Formations" inManagedObjectContext:_managedObjectContext];
[request setEntity:entity];
NSSortDescriptor *sort = [[NSSortDescriptor alloc] initWithKey:@"sections.sortNB" ascending:YES];
[request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"sections", nil]];
[request setSortDescriptors:[NSArray arrayWithObject:sort]];
NSArray *forms = [[_managedObjectContext executeFetchRequest:request error:nil]mutableCopy];
但出现错误:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'
我该如何继续?
我也想在我的章节实体中插入相同的数字以对其进行排序,但首先,让我们为章节解决它:p
谢谢
【问题讨论】:
-
那么你的
sortNB到底是什么,你在哪一行得到错误?您可能会将一组内容与单个内容进行比较。 -
在最后一行执行请求时出现错误。 sortNB 是字符串类型,但如果需要,我可以将其转换为 NSNumber。每个编队有 1 或 n 个部分,包含 1 或 n 个章节
-
您正在获取
Formations对象,这些对象无法按sections.sortNB排序,因为每个结构都与多个部分相关。
标签: ios xcode nsfetchrequest nssortdescriptor