【问题标题】:NSSortDescriptor with relationshipNSSortDescriptor 与关系
【发布时间】: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


【解决方案1】:

好的,所以我不是在我的请求中而是在使用之后对我的对象进行排序

NSArray *sortDescriptors = [NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"sortNB" ascending:YES]];

NSArray *sortedRecipes = [[[formation sections] allObjects] sortedArrayUsingDescriptors:sortDescriptors];

它有效。

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多