【发布时间】:2013-01-14 21:30:06
【问题描述】:
我有一个表格视图控制器,其中包含一个由 fetchResultsController 处理的 Formations 列表。
这是我的核心数据实体的样子:
我尝试按dateRange对我的 fetchResultsController 进行排序,如下所示:
// |fetchedResultsController| custom setter
- (NSFetchedResultsController *)fetchedResultsController {
if (_fetchedResultsController != nil) {
return _fetchedResultsController;
}
_fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:self.fetchRequest managedObjectContext:self.mainManagedObjectContext sectionNameKeyPath:@"dateRange" cacheName:kFormationsFetchedCacheName];
_fetchedResultsController.delegate = self;
return _fetchedResultsController;
}
// |fetchRequest| custom setter
- (NSFetchRequest *)fetchRequest {
if (_fetchRequest != nil) {
return _fetchRequest;
}
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"student == %@", self.currentStudent];
NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"dateRange" ascending:NO];
NSSortDescriptor *nameDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:NO];
_fetchRequest = [[NSFetchRequest alloc] initWithEntityName:kBSFormation];
_fetchRequest.predicate = predicate;
_fetchRequest.sortDescriptors = [NSArray arrayWithObjects: dateDescriptor, nameDescriptor, nil];
return _fetchRequest;
}
当我尝试添加第一个 Formation 时,一切都很好,但对于接下来的,我有这些错误:
2013-01-30 22:43:08.370 [7202:c07] -[BSDateRange compare:]: unrecognized selector sent to instance 0x81781a0
2013-01-30 22:43:08.371 [7202:c07] CoreData: error: Serious application error. Exception was caught during Core Data change processing. This is usually a bug within an observer of NSManagedObjectContextObjectsDidChangeNotification. -[BSDateRange compare:]: unrecognized selector sent to instance 0x81781a0 with userInfo (null)
2013-01-30 22:43:08.372 [7202:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[BSDateRange compare:]: unrecognized selector sent to instance 0x81781a0'
如果我评论这一行:NSSortDescriptor *dateDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"dateRange" ascending:NO];,它可以工作,但我的表格视图很乱,因为sectionNameKeyPath 设置为dateRange
有人正在弄清楚这里的问题是什么? :/
【问题讨论】:
-
跳出来的第一件事是你试图对关系而不是属性进行排序?
-
是的,不知道是否允许这样做,但由于我也在我的部分中使用这种关系,我认为我必须使用相同的关系进行排序才能正确获取我的表格视图
标签: ios exception core-data nsfetchedresultscontroller nssortdescriptor