-
Notifications
You must be signed in to change notification settings - Fork 1
/
RBFetchedResultsTableVC.m
250 lines (189 loc) · 8.06 KB
/
RBFetchedResultsTableVC.m
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
//
// RBFetchedResultsTableVC.m
//
// Copyright (c) 2011 Robert Brown
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
#import "RBFetchedResultsTableVC.h"
#import "RBCoreDataManager.h"
@interface RBFetchedResultsTableVC () {
@private
NSManagedObjectContext * _context;
NSFetchedResultsController * _fetchedResultsController;
}
@property (nonatomic, strong, readwrite) NSFetchedResultsController * fetchedResultsController;
@property (nonatomic, strong, readwrite) NSManagedObjectContext * context;
/**
* Required method, must be overriden by subclasses. Return the name of the entity
* you want to display in the tableview.
*/
- (NSString *)entityName;
/**
* Returns the predicate to use to filter the fetch.
*/
- (NSPredicate *)predicate;
/**
* The number of results to return in one fetch. Set to slightly more than one
* screen full of content.
*/
- (NSInteger)batchSize;
/**
* Return any sort descriptors you want to use to sort your data.
*/
- (NSArray *)sortDescriptors;
/**
* The name to use for a cache. If you don't want to cache your data, then
* return nil.
*/
- (NSString *)cacheName;
/**
* Return the keypath to use for sections. If you don't want to use sections,
* return nil.
*/
- (NSString *)sectionNameKeyPath;
@end
@implementation RBFetchedResultsTableVC
@synthesize tableView = _tableView;
@synthesize fetchedResultsController = _fetchedResultsController;
@synthesize context = _context;
- (NSManagedObjectContext *)context {
// Lazy getter.
if (!_context) {
@synchronized(self) {
if (!_context)
[self setContext:[[RBCoreDataManager defaultManager] createMOC]];
}
}
return _context;
}
#pragma mark - Template methods
- (NSString *)entityName {
NSAssert1(NO, @"Required method: %@ not overriden.", NSStringFromSelector(@selector(_cmd)));
return nil;
}
- (NSPredicate *)predicate {
return nil;
}
- (NSInteger)batchSize {
return 20;
}
- (NSArray *)sortDescriptors {
NSAssert1(NO, @"Required method: %@ not overriden.", NSStringFromSelector(@selector(_cmd)));
return nil;
}
- (NSString *)cacheName {
return NSStringFromClass([self class]);
}
- (NSString *)sectionNameKeyPath {
return nil;
}
#pragma mark - Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
return [[[self fetchedResultsController] sections] count];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
id <NSFetchedResultsSectionInfo> sectionInfo = [[[self fetchedResultsController] sections] objectAtIndex:section];
return [sectionInfo numberOfObjects];
}
- (UITableViewCell *)tableView:(UITableView *)aTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
NSAssert1(NO, @"Required method: %@ not overriden.", NSStringFromSelector(@selector(_cmd)));
return nil;
}
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
return NO;
}
#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
// Subclasses should override this method.
}
#pragma mark - Fetched results controller
- (NSFetchedResultsController *)fetchedResultsController {
@synchronized(self) {
if (_fetchedResultsController)
return _fetchedResultsController;
// Create the fetch request for the entity.
NSFetchRequest * fetchRequest = [NSFetchRequest new];
// Edit the entity name as appropriate.
NSEntityDescription * entity = [NSEntityDescription entityForName:[self entityName]
inManagedObjectContext:[self context]];
[fetchRequest setEntity:entity];
[fetchRequest setFetchBatchSize:[self batchSize]];
[fetchRequest setSortDescriptors:[self sortDescriptors]];
[fetchRequest setPredicate:[self predicate]];
NSFetchedResultsController * fetchController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest
managedObjectContext:[self context]
sectionNameKeyPath:[self sectionNameKeyPath]
cacheName:[self cacheName]];
fetchController.delegate = self;
_fetchedResultsController = fetchController;
NSError * error = nil;
if (![_fetchedResultsController performFetch:&error]) {
// !!!: Handle this error however you want.
NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
abort();
}
}
return _fetchedResultsController;
}
#pragma mark - Fetched results controller delegate
- (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;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject
atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type
newIndexPath:(NSIndexPath *)newIndexPath {
UITableView * aTableView = [self tableView];
switch(type) {
case NSFetchedResultsChangeInsert:
[aTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeDelete:
[aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
case NSFetchedResultsChangeUpdate:
[[aTableView dataSource] tableView:aTableView cellForRowAtIndexPath:indexPath];
break;
case NSFetchedResultsChangeMove:
[aTableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath]
withRowAnimation:UITableViewRowAnimationFade];
[aTableView insertRowsAtIndexPaths:[NSArray arrayWithObject:newIndexPath]
withRowAnimation:UITableViewRowAnimationFade];
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.tableView endUpdates];
}
@end