-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJxFetchResultCollectionViewController.m
More file actions
347 lines (275 loc) · 11.8 KB
/
JxFetchResultCollectionViewController.m
File metadata and controls
347 lines (275 loc) · 11.8 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
//
// JxFetchResultCollectionViewController.h
//
// Created by Jeanette Müller on 16.08.13.
//
#import "JxFetchResultCollectionViewController.h"
#import "Logging.h"
@interface JxFetchResultCollectionViewController ()
@property (assign, nonatomic) BOOL shouldReloadCollectionView;
@property (strong, nonatomic) NSBlockOperation *blockOperation;
@property (strong, nonatomic) NSMutableArray *objectChanges;
@property (strong, nonatomic) NSMutableArray *sectionChanges;
- (void)configureCell:(UICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath;
@end
@implementation JxFetchResultCollectionViewController{
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.objectChanges = [NSMutableArray array];
self.sectionChanges = [NSMutableArray array];
}
- (void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
LLog();
if (!_collectionView) {
NSLog(@"\n\n\nWARNING: please connect your UICollectionView with the Interface Builder to this Controller\n\n\n");
}
[_collectionView setScrollsToTop:YES];
}
- (void)viewWillDisappear:(BOOL)animated{
[_collectionView setScrollsToTop:NO];
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
[super viewWillDisappear:animated];
}
- (void)viewWillLayoutSubviews
{
[super viewWillLayoutSubviews];
[self.collectionView.collectionViewLayout invalidateLayout];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
[collectionView.collectionViewLayout invalidateLayout];
return [[self.fetchedResultsController sections] count];
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
id <NSFetchedResultsSectionInfo> sectionInfo = [self.fetchedResultsController sections][section];
NSInteger count = [sectionInfo numberOfObjects];
DLog(@"count %ld", (long)count);
return count;
}
// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"CollectionCell" forIndexPath:indexPath];
[self configureCell:cell atIndexPath:indexPath];
return cell;
}
- (void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
[self unloadCell:cell];
}
- (void)configureCell:(UICollectionViewCell *)cell atIndexPath:(NSIndexPath *)indexPath{
LLog();
NSManagedObject *object = [self.fetchedResultsController objectAtIndexPath:indexPath];
NSLog(@"object id: %@", object.objectID);
}
- (void)unloadCell:(UICollectionViewCell *)cell{
LLog();
//do something like remove observers
[[NSNotificationCenter defaultCenter] removeObserver:cell];
}
//- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id <NSFetchedResultsSectionInfo>)section InfoatIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type{
// LLog();
// if ([[self.navigationController.viewControllers lastObject] isEqual:self]){
//
// switch(type) {
// case NSFetchedResultsChangeInsert:
//
// [self.collectionView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
// //change[@(type)] = @(sectionIndex);
// break;
// case NSFetchedResultsChangeDelete:
// [self.collectionView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
// //change[@(type)] = @(sectionIndex);
// break;
// default:
// break;
// }
//
// }
//}
//- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath{
// LLog();
// if ([[self.navigationController.viewControllers lastObject] isEqual:self]){
// switch(type) {
//
// case NSFetchedResultsChangeInsert:
// if (newIndexPath.section > self.collectionView.numberOfSections-1) {
// [self.collectionView insertSections:[NSIndexSet indexSetWithIndex:newIndexPath.section]];
// }
//
// [self.collectionView insertItemsAtIndexPaths:@[newIndexPath]];
// break;
//
// case NSFetchedResultsChangeDelete:
// NSLog(@"didChangeObject - delete");
// [self.collectionView deleteItemsAtIndexPaths:@[indexPath]];
// break;
//
// case NSFetchedResultsChangeUpdate:
// NSLog(@"didChangeObject - update");
//
// [_collectionView reloadItemsAtIndexPaths:@[indexPath]];
// break;
//
// case NSFetchedResultsChangeMove:
// NSLog(@"didChangeObject - move");
//
// [_collectionView moveItemAtIndexPath:indexPath toIndexPath:newIndexPath];
//
//// if (indexPath.section == newIndexPath.section) {
//// [_collectionView moveItemAtIndexPath:indexPath toIndexPath:newIndexPath];
//// [_collectionView reloadItemsAtIndexPaths:@[newIndexPath]];
//// }else{
//// [self.collectionView reloadData];
//// }
// break;
// }
// }
//}
//- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller{
// LLog();
//
// //bei weiterne problemen könnte das helfen
// //[NSFetchedResultsController deleteCacheWithName:controller.cacheName];
//}
//- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller{
// LLog();
// //[self.collectionView reloadData];
//}
- (BOOL)shouldReloadCollectionViewToPreventKnownIssue {
LLog();
__block BOOL shouldReload = NO;
for (NSDictionary *change in _objectChanges) {
[change enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSFetchedResultsChangeType type = [key unsignedIntegerValue];
NSIndexPath *indexPath = obj;
switch (type) {
case NSFetchedResultsChangeInsert:
if ([_collectionView numberOfItemsInSection:indexPath.section] == 0) {
shouldReload = YES;
} else {
shouldReload = NO;
}
break;
case NSFetchedResultsChangeDelete:
if ([_collectionView numberOfItemsInSection:indexPath.section] == 1) {
shouldReload = YES;
} else {
shouldReload = NO;
}
break;
case NSFetchedResultsChangeUpdate:
shouldReload = NO;
break;
case NSFetchedResultsChangeMove:
shouldReload = NO;
break;
}
}];
}
return shouldReload;
}
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation{
LLog();
[_collectionView performBatchUpdates:nil completion:nil];
}
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
//#warning provisorisch auf YES gesetzt da update nicht richtig klappen wollte
self.shouldReloadCollectionView = NO;
self.blockOperation = [[NSBlockOperation alloc] init];
}
- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo
atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
{
__weak UICollectionView *collectionView = self.collectionView;
switch (type) {
case NSFetchedResultsChangeInsert: {
[self.blockOperation addExecutionBlock:^{
[collectionView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
}];
break;
}
case NSFetchedResultsChangeDelete: {
[self.blockOperation addExecutionBlock:^{
[collectionView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
}];
break;
}
case NSFetchedResultsChangeUpdate: {
[self.blockOperation addExecutionBlock:^{
[collectionView reloadSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
}];
break;
}
default:
break;
}
}
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
__weak UICollectionView *collectionView = self.collectionView;
switch (type) {
case NSFetchedResultsChangeInsert: {
if ([self.collectionView numberOfSections] > indexPath.section && [self.collectionView numberOfSections] > newIndexPath.section) {
if ([self.collectionView numberOfItemsInSection:indexPath.section] == 0) {
self.shouldReloadCollectionView = YES;
} else {
[self.blockOperation addExecutionBlock:^{
[collectionView insertItemsAtIndexPaths:@[newIndexPath]];
}];
}
} else {
self.shouldReloadCollectionView = YES;
}
break;
}
case NSFetchedResultsChangeDelete: {
if ([self.collectionView numberOfItemsInSection:indexPath.section] == 1) {
self.shouldReloadCollectionView = YES;
} else {
[self.blockOperation addExecutionBlock:^{
[collectionView deleteItemsAtIndexPaths:@[indexPath]];
}];
}
break;
}
case NSFetchedResultsChangeUpdate: {
// [collectionView reloadItemsAtIndexPaths:@[indexPath]];
if ([self.collectionView numberOfSections] > newIndexPath.section) {
if ([self.collectionView numberOfItemsInSection:newIndexPath.section] == 0) {
self.shouldReloadCollectionView = YES;
} else {
[self.blockOperation addExecutionBlock:^{
[collectionView deleteItemsAtIndexPaths:@[indexPath]];
[collectionView insertItemsAtIndexPaths:@[newIndexPath]];
}];
}
}else {
self.shouldReloadCollectionView = YES;
}
break;
}
case NSFetchedResultsChangeMove: {
[self.blockOperation addExecutionBlock:^{
[collectionView moveItemAtIndexPath:indexPath toIndexPath:newIndexPath];
}];
break;
}
default:
break;
}
}
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
// Checks if we should reload the collection view to fix a bug @ http://openradar.appspot.com/12954582
if (self.shouldReloadCollectionView) {
[self.collectionView reloadData];
} else {
[self.collectionView performBatchUpdates:^{
[self.blockOperation start];
} completion:nil];
}
}
@end