【问题标题】:iPhone: Unable to NSSortDescriptor on to-many relationship. How to sort?iPhone:无法对多对关系进行 NSSortDescriptor。如何排序?
【发布时间】:2012-11-19 20:32:16
【问题描述】:

我正在使用通过 NSSortDescriptor 排序的 NSFetchRequest。

这对于获取的实体的所有属性都非常有效。

在该实体中只有一个问题(不是一直存在吗?)我与另一个实体有关系,我也想对这个问题进行排序。但它给了我以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'to-many key not allowed here'

这是我的核心数据设置:

这是我的代码:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
    NSEntityDescription *entityMonth = [NSEntityDescription entityForName:@"Month" inManagedObjectContext:_managedObjectContext];
    [fetchRequest setEntity:entityMonth];

    NSSortDescriptor *sortDescriptorItem = [NSSortDescriptor sortDescriptorWithKey:@"item.nr" ascending:YES]; //THIS ONE CAUSES THE ERROR!
    NSSortDescriptor *sortDescriptorMonth = [[NSSortDescriptor alloc]initWithKey:@"month" ascending:YES];
    NSSortDescriptor *sortDescriptorYear = [[NSSortDescriptor alloc]initWithKey:@"year" ascending:YES];


    NSArray *sortDescriptors = [NSArray arrayWithObjects:sortDescriptorYear, sortDescriptorMonth, sortDescriptorItem, nil];
    [fetchRequest setSortDescriptors:sortDescriptors];

    NSError *error = nil;
    self.stages = [_managedObjectContext executeFetchRequest:fetchRequest error:&error];

【问题讨论】:

    标签: iphone core-data entity-relationship nsfetchrequest nssortdescriptor


    【解决方案1】:

    您无法对item.nr 进行排序,因为Month 对象与许多Item 对象相关,因此每个Month 对象都有多个item.nr 值。

    【讨论】:

    • 如果我想根据 nr 属性对所有项目进行排序,我应该如何处理呢?
    • @iJar:您的 fetch 请求返回 Month 对象,而不是 Item 对象,因此我还不明白您到底想要什么。
    • 此数据显示在表格视图中。 month.name 显示为部分标题,项目是每个部分下的行。 month.year 和month.month 对部分进行排序。现在我正在寻找一种基于 item.id 对行进行排序的方法。希望这能澄清我的意图。
    • @iJar:你使用获取结果控制器吗?
    • 不,我现在不在。这能帮上忙吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多