Created
June 28, 2013 07:37
-
-
Save advantis/5883110 to your computer and use it in GitHub Desktop.
Generic NSFetchedResultsControllerDelegate implementations
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
// | |
// Copyright © 2013 Yuri Kotov | |
// | |
#import <CoreData/CoreData.h> | |
@interface ADVManagedCollectionUpdater : NSObject <NSFetchedResultsControllerDelegate> | |
@property (weak, nonatomic) IBOutlet UICollectionView *collectionView; | |
@end |
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
// | |
// Copyright © 2013 Yuri Kotov | |
// | |
#import "ADVManagedCollectionUpdater.h" | |
typedef void (^ADVCollectionViewUpdateBlock)(); | |
@interface ADVManagedCollectionUpdater () | |
@property (strong, nonatomic) NSMutableSet *updates; | |
@end | |
@implementation ADVManagedCollectionUpdater | |
#pragma mark - NSFetchedResultsControllerDelegate | |
- (void) controllerWillChangeContent:(NSFetchedResultsController *)controller | |
{ | |
self.updates = [NSMutableSet new]; | |
} | |
- (void) controller:(NSFetchedResultsController *)controller | |
didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo | |
atIndex:(NSUInteger)sectionIndex | |
forChangeType:(NSFetchedResultsChangeType)type | |
{ | |
ADVCollectionViewUpdateBlock update; | |
__weak UICollectionView *collectionView = self.collectionView; | |
NSIndexSet *sections = [NSIndexSet indexSetWithIndex:sectionIndex]; | |
switch (type) | |
{ | |
case NSFetchedResultsChangeInsert: { | |
update = ^{ | |
[collectionView insertSections:sections]; | |
}; | |
break; | |
} | |
case NSFetchedResultsChangeDelete: { | |
update = ^{ | |
[collectionView deleteSections:sections]; | |
}; | |
break; | |
} | |
case NSFetchedResultsChangeMove: | |
// Nothing to do here | |
return; | |
case NSFetchedResultsChangeUpdate: { | |
update = ^{ | |
[collectionView reloadSections:sections]; | |
}; | |
break; | |
} | |
} | |
[self.updates addObject:update]; | |
} | |
- (void) controller:(NSFetchedResultsController *)controller | |
didChangeObject:(id)anObject | |
atIndexPath:(NSIndexPath *)indexPath | |
forChangeType:(NSFetchedResultsChangeType)type | |
newIndexPath:(NSIndexPath *)newIndexPath | |
{ | |
ADVCollectionViewUpdateBlock update; | |
__weak UICollectionView *collectionView = self.collectionView; | |
switch (type) | |
{ | |
case NSFetchedResultsChangeInsert: { | |
update = ^{ | |
[collectionView insertItemsAtIndexPaths:@[newIndexPath]]; | |
}; | |
break; | |
} | |
case NSFetchedResultsChangeDelete: { | |
update = ^{ | |
[collectionView deleteItemsAtIndexPaths:@[indexPath]]; | |
}; | |
break; | |
} | |
case NSFetchedResultsChangeUpdate: { | |
update = ^{ | |
[collectionView reloadItemsAtIndexPaths:@[indexPath]]; | |
}; | |
break; | |
} | |
case NSFetchedResultsChangeMove: { | |
update = ^{ | |
[collectionView moveItemAtIndexPath:indexPath toIndexPath:newIndexPath]; | |
}; | |
break; | |
} | |
} | |
[self.updates addObject:update]; | |
} | |
- (void) controllerDidChangeContent:(NSFetchedResultsController *)controller | |
{ | |
[self.collectionView performBatchUpdates:^{ | |
for (ADVCollectionViewUpdateBlock update in self.updates) update(); | |
} completion:^(BOOL finished) { | |
self.updates = nil; | |
}]; | |
} | |
@end |
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
// | |
// Copyright © 2013 Yuri Kotov | |
// | |
#import <CoreData/CoreData.h> | |
@interface ADVManagedTableUpdater : NSObject <NSFetchedResultsControllerDelegate> | |
@property (weak, nonatomic) IBOutlet UITableView *tableView; | |
@end |
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
// | |
// Copyright © 2013 Yuri Kotov | |
// | |
#import "ADVManagedTableUpdater.h" | |
@implementation ADVManagedTableUpdater | |
#pragma mark - NSFetchedResultsControllerDelegate | |
- (void) controllerWillChangeContent:(NSFetchedResultsController *)controller | |
{ | |
[self.tableView beginUpdates]; | |
} | |
- (void) controller:(NSFetchedResultsController *)controller | |
didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo | |
atIndex:(NSUInteger)sectionIndex | |
forChangeType:(NSFetchedResultsChangeType)type | |
{ | |
UITableView *tableView = self.tableView; | |
NSIndexSet *sections = [NSIndexSet indexSetWithIndex:sectionIndex]; | |
switch (type) | |
{ | |
case NSFetchedResultsChangeInsert: | |
[tableView insertSections:sections withRowAnimation:UITableViewRowAnimationAutomatic]; | |
break; | |
case NSFetchedResultsChangeDelete: | |
[tableView deleteSections:sections withRowAnimation:UITableViewRowAnimationAutomatic]; | |
break; | |
case NSFetchedResultsChangeMove: | |
// Nothing to do here | |
return; | |
case NSFetchedResultsChangeUpdate: | |
[tableView reloadSections:sections withRowAnimation:UITableViewRowAnimationAutomatic]; | |
break; | |
} | |
} | |
- (void) controller:(NSFetchedResultsController *)controller | |
didChangeObject:(id)anObject | |
atIndexPath:(NSIndexPath *)indexPath | |
forChangeType:(NSFetchedResultsChangeType)type | |
newIndexPath:(NSIndexPath *)newIndexPath | |
{ | |
UITableView *tableView = self.tableView; | |
switch (type) | |
{ | |
case NSFetchedResultsChangeInsert: | |
[tableView insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; | |
break; | |
case NSFetchedResultsChangeDelete: | |
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; | |
break; | |
case NSFetchedResultsChangeUpdate: | |
[tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic]; | |
break; | |
case NSFetchedResultsChangeMove: | |
[tableView moveRowAtIndexPath:indexPath toIndexPath:newIndexPath]; | |
break; | |
} | |
} | |
- (void) controllerDidChangeContent:(NSFetchedResultsController *)controller | |
{ | |
[self.tableView endUpdates]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment