次のエラー メッセージが表示されます。
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) の招待者が 2 人いる場合に絞り込んだようで、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;
次に、sortDescriptors を追加する buildFetchRequest の一部を示します。
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];
どんなアイデアでも、これは私を夢中にさせています。