我收到以下错误消息:
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上浏览了无数个答案,最终大多指向使用case-Insensive 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中的一段,它添加了sortDescators:
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];
任何想法,这都快把我逼疯了。
您正在设置一个不同于第一个排序描述符键的提取结果控制器部分名称键路径。
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
。
这应该会导致您看到的错误。
好吧,关键问题是哪个属性决定sectionNameKeyPath以及该属性是否已被用作排序描述符数组中的第一个排序描述符。
我想这是检查你是否有这样的问题的第一件事。