Skip to content

Instantly share code, notes, and snippets.

@yeonsh
Created April 4, 2011 01:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yeonsh/901016 to your computer and use it in GitHub Desktop.
Save yeonsh/901016 to your computer and use it in GitHub Desktop.
-(BOOL) setupFetchedResultsControllerForEntity:(NSString *)argEntityName withSortKey:(NSString *)argSortKey
{
// Uncomment the following line to display an Edit button in the navigation bar for this view controller.
// self.navigationItem.rightBarButtonItem = self.editButtonItem;
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
// Configure the request's entity, and optionally its predicate.
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:argEntityName inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entityDescription];
NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:argSortKey ascending:NO];
NSArray *sortDescriptors = [[NSArray alloc] initWithObjects:sortDescriptor, nil];
[fetchRequest setSortDescriptors:sortDescriptors];
[sortDescriptors release];
[sortDescriptor release];
self.fetchedResultsController = [[NSFetchedResultsController alloc]
initWithFetchRequest:fetchRequest
managedObjectContext:self.managedObjectContext
sectionNameKeyPath:nil
cacheName:nil];
[self.fetchedResultsController setDelegate:self];
[fetchRequest release];
NSError *error;
BOOL success = [self.fetchedResultsController performFetch:&error];
if (success) {
T_DBG(@"fetch succeeded.");
return YES;
}
else {
T_DBG(@"fetch failed");
return NO;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment