我收到以下错误消息:
CoreData: error: (NSFetchedResultsController) The fetched object at index 5 has an out of order section name 'James. Objects must be sorted by section name'
Unresolved search error Error Domain=NSCocoaErrorDomain Code=134060 "The operation couldn’t be completed. (Cocoa error 134060.)" UserInfo=0xaa07530 {reason=The fetched object at index 5 has an out of order section name 'James. Objects must be sorted by section name'}, {
reason = "The fetched object at index 5 has an out of order section name 'James. Objects must be sorted by section name'";
}
我已经在SO上经历了无数的答案,这些答案主要指向使用caseInsensitive compare作为答案,但是如下所示,我的提取请求已经设置为执行此操作:
<NSFetchRequest: 0xc082bd0> (entity: InviteeModel; predicate: (eventId == 148 AND typeOf != "InviteeTypeOfServerContact"); sortDescriptors: ((
"(lastName, ascending, caseInsensitiveCompare:)",
"(firstName, ascending, caseInsensitiveCompare:)"
)); batch size: 1000; type: NSManagedObjectResultType; )
我似乎把它缩小到我有两个姓氏相同的受邀者(即亨利·詹姆斯和亨利·詹姆斯)的情况下,然后我得到上面的错误,说詹姆斯出了故障。如果我将这两个姓氏改为詹姆斯或詹姆斯,那么它有效吗?!?!?
以下是创建获取请求的代码:
NSFetchRequest *fetchRequest = [self buildFetchRequest];
// We have to reset the fetched results controller if the sort changes.
// Because otherwise the scrolling index will be inaccurate.
if (self.fetchedResultsController) {
if (![self.fetchedResultsController.sectionNameKeyPath isEqualToString:self.sortBy]) {
self.fetchedResultsController = nil;
}
}
if (!self.fetchedResultsController) {
NSManagedObjectContext *context = self.event.managedObjectContext;
NSString *sectionNameKeyPath = nil;
if (self.sortBy == DefaultSortBy) {
sectionNameKeyPath = @"sectionChar";
}
else {
sectionNameKeyPath = self.sortBy;
if ([self.sortBy isEqualToString:@"rsvpState"] || [self.sortBy isEqualToString:@"checkedIn"]) {
sectionNameKeyPath = @"lastName";
}
}
NSFetchedResultsController *fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:context
sectionNameKeyPath:sectionNameKeyPath
cacheName:nil];
fetchedResultsController.delegate = self;
self.fetchedResultsController = fetchedResultsController;
以下是buildFetchRequest中添加了sortDescriptors:
的部分 NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:self.sortBy
ascending:self.shouldSortAscending
selector:@selector(caseInsensitiveCompare:)];
// Add the default sorts (last, first).
NSSortDescriptor *lastSort = nil;
NSSortDescriptor *firstSort = nil;
if (![self.sortBy isEqualToString:@"lastName"]) {
lastSort = [NSSortDescriptor sortDescriptorWithKey:@"lastName"
ascending:YES
selector:@selector(caseInsensitiveCompare:)];
}
if (![self.sortBy isEqualToString:@"firstName"]) {
firstSort = [NSSortDescriptor sortDescriptorWithKey:@"firstName"
ascending:YES
selector:@selector(caseInsensitiveCompare:)];
}
NSArray *sorts = nil;
if (lastSort && firstSort) {
sorts = [[NSArray alloc] initWithObjects:sortDescriptor, lastSort, firstSort, nil];
}
else if (lastSort) {
sorts = [[NSArray alloc] initWithObjects:sortDescriptor, lastSort, nil];
}
else {
sorts = [[NSArray alloc] initWithObjects:sortDescriptor, firstSort, nil];
}
[fetchRequest setSortDescriptors:sorts];
任何想法,这都让我发疯。
答案 0 :(得分:2)
您正在设置一个与第一个排序描述符键不同的获取结果控制器部分名称键路径。
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:self.sortBy
ascending:self.shouldSortAscending
selector:@selector(caseInsensitiveCompare:)];
键是self.sortBy
。
if ([self.sortBy isEqualToString:@"rsvpState"] ||
[self.sortBy isEqualToString:@"checkedIn"]) {
sectionNameKeyPath = @"lastName";
}
键不 self.sortBy
。
这会导致您看到的错误。
答案 1 :(得分:0)
那么,关键问题是哪个属性正在确定sectionNameKeyPath 如果该属性已被用作排序描述符数组中的第一个排序描述符。
我想这是第一件检查你是否遇到这样的问题。