我收到以下错误消息:
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比较作为答案,但是正如您在下面看到的那样,我的提取请求已设置为执行此操作:
<NSFetchRequest: 0xc082bd0> (entity: InviteeModel; predicate: (eventId == 148 AND typeOf != "InviteeTypeOfServerContact"); sortDescriptors: ((
"(lastName, ascending, caseInsensitiveCompare:)",
"(firstName, ascending, caseInsensitiveCompare:)"
)); batch size: 1000; type: NSManagedObjectResultType; )
我似乎已将其范围缩小到以下情况:我有两个姓氏相同的被邀请人(即HENRY JAMES和Henry James),然后我得到一个错误,上面是说James出故障了。如果我将两个姓氏都更改为James或JAMES,那么它起作用吗?
这是创建获取请求的代码:
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];
任何想法,这使我发疯。
最佳答案
您正在设置与第一排序描述符键不同的访存结果 Controller 节名称键路径。
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc]
initWithKey:self.sortBy
ascending:self.shouldSortAscending
selector:@selector(caseInsensitiveCompare:)];
key 是
self.sortBy
。if ([self.sortBy isEqualToString:@"rsvpState"] ||
[self.sortBy isEqualToString:@"checkedIn"]) {
sectionNameKeyPath = @"lastName";
}
key 是而不是
self.sortBy
。这应该导致您看到的错误。
关于ios - 奇怪的问题- "The fetched object at index x has an out of order section name",我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18343312/