Created
December 1, 2010 22:11
-
-
Save gfontenot/724326 to your computer and use it in GitHub Desktop.
Method to change to NSFetchedResultsController dynamically
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-(void)createFetchedResultsControllerWithSortOption:(NSInteger)sortOption animated:(BOOL)animated { | |
self.fetchedResultsController.delegate = nil; | |
self.fetchedResultsController = nil; | |
[fetchedResultsController release]; | |
if (animated) { | |
NSMutableIndexSet *sectionsToDelete = [[NSMutableIndexSet alloc] init]; | |
for (int i = 0; i < [self.tableView numberOfSections]; i ++) { | |
[sectionsToDelete addIndex:i]; | |
} | |
if ([sectionsToDelete count] > 0) { | |
[self.tableView deleteSections:sectionsToDelete withRowAnimation:UITableViewRowAnimationLeft]; | |
} | |
[sectionsToDelete release]; | |
} | |
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init]; | |
NSEntityDescription *entity = [NSEntityDescription entityForName:@"Person" inManagedObjectContext:managedObjectContext]; | |
[fetchRequest setEntity:entity]; | |
NSString *firstAttribute = nil; | |
NSString *secondAttribute = nil; | |
BOOL ascending = YES; | |
NSString *cacheName = nil; | |
BOOL hasSectionKey = YES; | |
switch (sortOption) { | |
case sortByDate: { // Working sort option | |
firstAttribute = @"date"; | |
ascending = NO; | |
cacheName = @"dateCache"; | |
hasSectionKey = NO; | |
} | |
break; | |
case sortByName: { // Working sort option | |
firstAttribute = @"name"; | |
cacheName = @"nameCache"; | |
hasSectionKey = NO; | |
} | |
break; | |
case sortByCompany: { // Working sort option | |
firstAttribute = @"company"; | |
secondAttribute = @"name"; | |
cacheName = @"companyCache"; | |
} | |
break; | |
case sortByJob: { // Broken sort option | |
firstAttribute = @"job"; | |
secondAttribute = @"name"; | |
cacheName = @"jobCache"; | |
} | |
break; | |
case sortByRating: { // Working sort option | |
firstAttribute = @"rating"; | |
secondAttribute = @"name"; | |
cacheName = @"ratingCache"; | |
} | |
break; | |
case sortByLocation: { // Broken sort option | |
firstAttribute = @"location"; | |
secondAttribute = @"company"; | |
cacheName = @"locationCache"; | |
} | |
break; | |
} | |
NSMutableArray *sortDescriptors = [[NSMutableArray alloc] initWithCapacity:2]; | |
NSSortDescriptor *mainSortDescriptor = [[NSSortDescriptor alloc] initWithKey:firstAttribute ascending:ascending]; | |
[sortDescriptors addObject:mainSortDescriptor]; | |
[mainSortDescriptor release]; | |
if (secondAttribute != nil) { | |
NSSortDescriptor *secondarySortDescriptor = [[NSSortDescriptor alloc] initWithKey:secondAttribute ascending:YES]; | |
[sortDescriptors addObject:secondarySortDescriptor]; | |
[secondarySortDescriptor release]; | |
} | |
[fetchRequest setSortDescriptors:sortDescriptors]; | |
NSFetchedResultsController *aFetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:managedObjectContext sectionNameKeyPath:(hasSectionKey) ? firstAttribute : nil cacheName:cacheName]; | |
self.fetchedResultsController = aFetchedResultsController; | |
self.fetchedResultsController.delegate = self; | |
[aFetchedResultsController release]; | |
[fetchRequest release]; | |
[sortDescriptors release]; | |
NSError *error; | |
if (![[self fetchedResultsController] performFetch:&error]) { | |
// Update to handle the error appropriately. | |
NSLog(@"Fetch failed"); | |
NSLog(@"Unresolved error %@, %@", error, [error userInfo]); | |
exit(-1); // Fail | |
} | |
if (animated) { | |
NSMutableIndexSet *sectionsToInsert = [[NSMutableIndexSet alloc] init]; | |
for (NSUInteger i = 0; i < [[fetchedResultsController sections] count]; i ++) { | |
[sectionsToInsert addIndex:i]; | |
} | |
if ([sectionsToInsert count] > 0) { | |
[self.tableView insertSections:sectionsToInsert withRowAnimation:UITableViewRowAnimationRight]; | |
} | |
[sectionsToInsert release]; | |
} else { | |
[self.tableView reloadData]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment