iPhone:无法与NSSortDescriptor建立多对多关系。怎么排序?

时间:2012-11-19 20:32:16

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

我正在使用NSFetchRequest,我通过 NSSortDescriptor 进行排序。

这项工作非常适合所提取实体的所有属性。

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

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

以下是我的核心数据设置:

enter image description 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];

1 个答案:

答案 0 :(得分:1)

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