索引X处的获取对象具有无序部分名称'X.对象必须按节名称排序'

时间:2013-01-20 03:43:35

标签: ios objective-c

我知道这已经涵盖在各种问题中,但我无法弄明白这一点。

据我所知,我的设置是正确的。我的第一个排序描述符与sectionNameKeyPath完全匹配。

我的客户很少发生此次崩溃,可能是因为它不在通常长时间打开的屏幕上。因为崩溃是罕见的,我根本无法重现它。

它在viewWillAppear-> table reloadData-> numberOfSectionsInTableView->访问器上崩溃,用于nsfetchedresultscontroller执行提取。

记录到我的崩溃记者的错误信息始终是相同的,但号码不同: 索引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.
}

0 个答案:

没有答案