Skip to content

Instantly share code, notes, and snippets.

@gonzalezreal
Created November 1, 2012 17:03
Show Gist options
  • Save gonzalezreal/3995060 to your computer and use it in GitHub Desktop.
Save gonzalezreal/3995060 to your computer and use it in GitHub Desktop.
// Based on Standford's CoreDataTableViewController
#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>
@interface TGRFetchedResultsTableViewController : UITableViewController <NSFetchedResultsControllerDelegate>
@property (strong, nonatomic) NSFetchedResultsController *fetchedResultsController;
@property (nonatomic) BOOL ignoreContentChanges;
- (void)performFetch;
@end
#import "TGRFetchedResultsTableViewController.h"
@interface TGRFetchedResultsTableViewController ()
@property (nonatomic) BOOL shouldEndTableViewUpdates;
@end
@implementation TGRFetchedResultsTableViewController
- (void)dealloc
{
self.fetchedResultsController = nil;
}
- (void)setFetchedResultsController:(NSFetchedResultsController *)fetchedResultsController
{
if (_fetchedResultsController != fetchedResultsController) {
_fetchedResultsController.delegate = nil;
_fetchedResultsController = fetchedResultsController;
if (_fetchedResultsController) {
_fetchedResultsController.delegate = self;
[self performFetch];
}
else {
[self.tableView reloadData];
}
}
}
- (void)setIgnoreContentChanges:(BOOL)ignore
{
if (ignore) {
_ignoreContentChanges = YES;
}
else {
[[NSOperationQueue mainQueue] addOperationWithBlock:^{
_ignoreContentChanges = NO;
}];
}
}
- (void)performFetch
{
if (self.fetchedResultsController) {
NSError *error = nil;
[self.fetchedResultsController performFetch:&error];
if (error) {
NSLog(@"Error performing fetch: %@", [error localizedDescription]);
}
}
[self.tableView reloadData];
}
#pragma mark - UITableViewDataSource methods
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] numberOfObjects];
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
return [[[self.fetchedResultsController sections] objectAtIndex:section] name];
}
- (NSInteger)tableView:(UITableView *)tableView sectionForSectionIndexTitle:(NSString *)title atIndex:(NSInteger)index
{
return [self.fetchedResultsController sectionForSectionIndexTitle:title atIndex:index];
}
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
return [self.fetchedResultsController sectionIndexTitles];
}
#pragma mark - NSFetchedResultsControllerDelegate methods
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
if (!self.ignoreContentChanges) {
[self.tableView beginUpdates];
self.shouldEndTableViewUpdates = YES;
}
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex
forChangeType:(NSFetchedResultsChangeType)type
{
if (!self.ignoreContentChanges) {
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
}
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
if (!self.ignoreContentChanges) {
switch (type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeUpdate:
[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
case NSFetchedResultsChangeMove:
[self.tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
[self.tableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
break;
}
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
if (self.shouldEndTableViewUpdates) {
[self.tableView endUpdates];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment