NSFetchedResultsController ERROR:
Afetr inserting a new record into my CoreData store using an NSFetchedResultsController, when I then try to display the data in a grouped UITableView the app crashes with the following error"
NSFetchedResultsController ERROR: The fetched object at index 5 has an out of order section name 'Navigation. Objects must be sorted by section name'
I have added the section key value to the sortDescriptors, but it doesn't help. Here is my code:
- (NSFetchedResultsController *)fetchedResultsController {
if (fetchedResultsController != nil) {
return fetchedResultsController;
}
/*
Set up the fetched results controller.
*/
// Create the fetch request for the entity.
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Edit the entity name as appropriate.
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Item" inManagedO开发者_如何转开发bjectContext:managedObjectContext];
[fetchRequest setEntity:entity];
// Set the batch size to a suitable number.
[fetchRequest setFetchBatchSize:20];
// Edit the sort key as appropriate.
NSSortDescriptor *sortByGroupName = [[NSSortDescriptor alloc] initWithKey:@"group.groupName" ascending:NO];
NSSortDescriptor *sortByIsMandatory = [[NSSortDescriptor alloc] initWithKey:@"isMandatory" ascending:NO];
NSSortDescriptor *sortByItemName = [[NSSortDescriptor alloc] initWithKey:@"itemName" ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortByGroupName, sortByIsMandatory, sortByItemName, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
// Edit the section name key path and cache name if appropriate.
// nil for section name key path means "no sections".
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:@"group.groupName" cacheName:@"Root"];
aFetchedResultsController.delegate = self;
self.fetchedResultsController = aFetchedResultsController;
[aFetchedResultsController release];
[fetchRequest release];
[sortDescriptors release];
[sortDescriptors release];
return fetchedResultsController;
}
What am I missing here?
Thanks --
Jk
Doh! Found my problem: I over-released the sortDescriptors
But you are leaking the individual sortDescriptors sortByGroupName, and so on. You should release those, too.
精彩评论