Skip to content
This repository has been archived by the owner on Jan 7, 2018. It is now read-only.

Commit

Permalink
Merge pull request #7 from cwagdev/master
Browse files Browse the repository at this point in the history
Known bug in UICollectionView causes exceptions and odd behaviors
  • Loading branch information
ashfurrow committed Feb 18, 2013
2 parents 3d4b848 + c38fcc2 commit eaf0af0
Showing 1 changed file with 70 additions and 24 deletions.
94 changes: 70 additions & 24 deletions AFMasterViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -176,35 +176,81 @@ - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller

if ([_objectChanges count] > 0 && [_sectionChanges count] == 0)
{
[self.collectionView performBatchUpdates:^{

if ([self shouldReloadCollectionViewToPreventKnownIssue]) {
// This is to prevent a bug in UICollectionView from occurring.
// The bug presents itself when inserting the first object or deleting the last object in a collection view.
// http://stackoverflow.com/questions/12611292/uicollectionview-assertion-failure
// This code should be removed once the bug has been fixed, it is tracked in OpenRadar
// http://openradar.appspot.com/12954582
[self.collectionView reloadData];

for (NSDictionary *change in _objectChanges)
{
[change enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id obj, BOOL *stop) {

NSFetchedResultsChangeType type = [key unsignedIntegerValue];
switch (type)
{
case NSFetchedResultsChangeInsert:
[self.collectionView insertItemsAtIndexPaths:@[obj]];
break;
case NSFetchedResultsChangeDelete:
[self.collectionView deleteItemsAtIndexPaths:@[obj]];
break;
case NSFetchedResultsChangeUpdate:
[self.collectionView reloadItemsAtIndexPaths:@[obj]];
break;
case NSFetchedResultsChangeMove:
[self.collectionView moveItemAtIndexPath:obj[0] toIndexPath:obj[1]];
break;
} else {

[self.collectionView performBatchUpdates:^{

for (NSDictionary *change in _objectChanges)
{
[change enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id obj, BOOL *stop) {

NSFetchedResultsChangeType type = [key unsignedIntegerValue];
switch (type)
{
case NSFetchedResultsChangeInsert:
[self.collectionView insertItemsAtIndexPaths:@[obj]];
break;
case NSFetchedResultsChangeDelete:
[self.collectionView deleteItemsAtIndexPaths:@[obj]];
break;
case NSFetchedResultsChangeUpdate:
[self.collectionView reloadItemsAtIndexPaths:@[obj]];
break;
case NSFetchedResultsChangeMove:
[self.collectionView moveItemAtIndexPath:obj[0] toIndexPath:obj[1]];
break;
}
}];
}
} completion:nil];
}

[_sectionChanges removeAllObjects];
[_objectChanges removeAllObjects];
}
}

- (BOOL)shouldReloadCollectionViewToPreventKnownIssue {
__block BOOL shouldReload = NO;
for (NSDictionary *change in self.objectChanges) {
[change enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
NSFetchedResultsChangeType type = [key unsignedIntegerValue];
NSIndexPath *indexPath = obj;
switch (type) {
case NSFetchedResultsChangeInsert:
if ([self.collectionView numberOfItemsInSection:indexPath.section] == 0) {
shouldReload = YES;
} else {
shouldReload = NO;
}
}];
break;
case NSFetchedResultsChangeDelete:
if ([self.collectionView numberOfItemsInSection:indexPath.section] == 1) {
shouldReload = YES;
} else {
shouldReload = NO;
}
break;
case NSFetchedResultsChangeUpdate:
shouldReload = NO;
break;
case NSFetchedResultsChangeMove:
shouldReload = NO;
break;
}
} completion:nil];
}];
}

[_sectionChanges removeAllObjects];
[_objectChanges removeAllObjects];
return shouldReload;
}

@end

0 comments on commit eaf0af0

Please sign in to comment.