-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMBCoreDataFetchControllerHelper.m
More file actions
88 lines (72 loc) · 2.78 KB
/
MBCoreDataFetchControllerHelper.m
File metadata and controls
88 lines (72 loc) · 2.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#import "MBCoreDataFetchControllerHelper.h"
#import <UIKit/UIKit.h>
@interface MBCoreDataFetchControllerHelper ()
@property (nonatomic, weak) UITableView *tableView;
@property (nonatomic, strong) void (^blockToExecuteWhenYouNeedToUpdateYourCell)(NSIndexPath *indexPath);
@end
@implementation MBCoreDataFetchControllerHelper
/* Designated initializer */
- (instancetype)initWithTableView:(UITableView *)aTableView
usingUpdateCellsBlock:(void(^)(NSIndexPath *indexPath))blockForUpdatingCells
{
self = [super init];
if (self) {
_tableView = aTableView;
_blockToExecuteWhenYouNeedToUpdateYourCell = blockForUpdatingCells;
}
return self;
}
- (instancetype)init
{
NSAssert(NO, @"USE DESIGNATED INITIALIZER");
return nil;
}
#pragma mark - NSFetchedResultsControllerDelegate
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView beginUpdates];
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeSection:(id <NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex
forChangeType:(NSFetchedResultsChangeType)type
{
switch(type) {
case NSFetchedResultsChangeInsert:
[self.tableView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[self.tableView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withRowAnimation:UITableViewRowAnimationFade];
break;
default:
return;
}
}
- (void)controller:(NSFetchedResultsController *)controller
didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath
forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath
{
UITableView *tableV = self.tableView;
switch(type) {
case NSFetchedResultsChangeInsert:
[tableV insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[tableV deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
self.blockToExecuteWhenYouNeedToUpdateYourCell(indexPath);
break;
case NSFetchedResultsChangeMove:
[tableV deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
[tableV insertRowsAtIndexPaths:@[newIndexPath] withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
[self.tableView endUpdates];
}
@end