gpt4 book ai didi

ios - NSFetchResultsController 错误与 sortDescriptors

转载 作者:行者123 更新时间:2023-11-28 22:40:44 25 4
gpt4 key购买 nike

我有一个 Table View Controller,其中包含由 fetchResultsController 处理的 Formation 列表。

这是我的核心数据实体的样子:

enter image description here

我尝试按 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];,它可以工作,但我的 TableView 很乱,因为 sectionNameKeyPath 设置为 dateRange

有人正在弄清楚这里的问题是什么? :/

最佳答案

您要告诉它根据 dateRange 关系进行排序。但是 dateRange 是与 BSDateRange 的关系,Core Data 如何比较它们?它应该使用 from,还是 to,或者它们的某种组合?您不能只告诉它对这样的对象进行排序,因为排序应该如何工作并不明显。

相反,首先弄清楚排序甚至意味着什么。然后适本地修改您的排序描述符。例如,如果您决定排序取决于 from 值,请更改排序描述符以使用 from 的关键路径:

NSSortDescriptor *dateDescriptor = [NSSortDescriptor
sortDescriptorWithKey:@"dateRange.from"
ascending:NO];

如果您需要根据fromto 进行排序,请使用多个排序描述符。

关于ios - NSFetchResultsController 错误与 sortDescriptors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14614627/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com