我正在使用NSFetchRequest,我通过 NSSortDescriptor 进行排序。
这项工作非常适合所提取实体的所有属性。
在该实体中只存在一个问题(总是不存在吗?)我与另一个实体有关系,我也想对这个问题进行排序。但它给了我以下错误:
*** 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];
答案 0 :(得分:1)
您无法对item.nr
进行排序,因为Month
对象与许多Item
个对象相关,因此每个item.nr
对象有多个Month
值