我知道这已被各种问题所涵盖,但我无法弄清楚这一点。
据我所知,我的设置是正确的。我的第一个排序描述符sectionNameKeyPath
完全匹配。
我的客户很少遇到这种崩溃,可能是因为它不在通常打开那么长时间的屏幕上。因为崩溃很少见,所以我根本无法重现它。
它在 viewWillAppear->table reloadData->numberOfSectionsInTableView->accessor for nsfetchedresultscontroller 执行 fetch 时崩溃。
它总是记录给我的崩溃报告者的相同错误消息,但编号不同: 索引 9 处的已获取对象有一个乱序部分名称 '2。对象必须按部分名称排序'
我使用 statusId 字段,它始终是 1-5 的数字。
代码:
- (NSFetchedResultsController *)unitsFetchedResultsController
{
if (unitsFetchedResultsController != nil) {
return unitsFetchedResultsController;
}
// SELECT * from Unit
NSFetchRequest* fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Unit" inManagedObjectContext:self.guestCard.managedObjectContext];
[fetchRequest setEntity:entity];
// make this more defensive by limiting the status ids (1-14ish are valid on the backend, we only display these)
NSPredicate *availablePredicate = [NSPredicate predicateWithFormat:@"available = 1"];
[fetchRequest setPredicate:availablePredicate];
// ORDER BY ready DESC, availableDate ASC (ATGT - must sort by 'ready' first or else 'sectionNameKeyPath' in the NSFetchedResultsController won't work)
NSSortDescriptor* readySortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"statusId" ascending:YES];
NSSortDescriptor* bedroomsSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"floorplan.bedrooms" ascending:YES];
NSSortDescriptor* bathroomsSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"floorplan.baths" ascending:YES];
NSSortDescriptor* availableDateSortDescriptor = [NSSortDescriptor sortDescriptorWithKey:@"availableDate" ascending:YES];
[fetchRequest setSortDescriptors:[NSArray arrayWithObjects:readySortDescriptor, bedroomsSortDescriptor, bathroomsSortDescriptor, availableDateSortDescriptor, nil]];
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.guestCard.managedObjectContext sectionNameKeyPath:@"statusId" cacheName:@"unitsAvailable"];
aFetchedResultsController.delegate = self;
self.unitsFetchedResultsController = aFetchedResultsController;
// Clean up
[aFetchedResultsController release];
[fetchRequest release];
NSError *error = nil;
if (![[self unitsFetchedResultsController] performFetch:&error]) {
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
[CoreDataErrorWriter writeCoreDataErrorToFile:error];
perimeterAppDelegate *appDelegate = APP_DELEGATE;
[appDelegate showCoreDataError];
[NSException raise:@"Core Data Error" format:@"Core Data Error in %@", NSStringFromClass([self class])];
}
return self.unitsFetchedResultsController; // I recognize I should just return the iVar, but not the problem.
}