samsoffes/sstoolkit — Issue #105: SSCollectionView doesn't function correctly when populated via NSFetchedResultsController
Author: P120D1GY
Created: 2011-11-18 05:05:10+00:00
State: open

Hello,

When trying to populate a SSCollectionView via NSFetchedResultsControllerDelegate methods, grid items don't appear during initial insertItemsAtIndexPaths between beginUpdates/endUpdates. Items are being created in NSFetchedResultsChangeInsert case, but nothing shows up. If I close the app then re-launch, grid items appear.

Code:

//=========================================================
// NSFetchedResultsControllerDelegate
//=========================================================
- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
[self.collectionView beginUpdates];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {

switch(type) {

case NSFetchedResultsChangeInsert: {
[self.collectionView insertItemsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withItemAnimation:SSCollectionViewItemAnimationFade];

break;
}

case NSFetchedResultsChangeDelete: {
[self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath] withItemAnimation:SSCollectionViewItemAnimationFade];

break;
}

case NSFetchedResultsChangeUpdate: {
[self configureItem:[self.collectionView itemForIndexPath:indexPath] atIndexPath:indexPath];

break;
}

case NSFetchedResultsChangeMove:
[self.collectionView deleteItemsAtIndexPaths:[NSArray arrayWithObject:indexPath] withItemAnimation:SSCollectionViewItemAnimationFade];

[self.collectionView insertItemsAtIndexPaths:[NSArray arrayWithObject:newIndexPath] withItemAnimation:SSCollectionViewItemAnimationFade];

break;
}
}

- (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id )sectionInfo atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {

switch(type) {

case NSFetchedResultsChangeInsert:
[self.collectionView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex] withItemAnimation:SSCollectionViewItemAnimationFade];
break;

case NSFetchedResultsChangeDelete:
[self.collectionView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex] withItemAnimation:SSCollectionViewItemAnimationFade];
break;
}
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
[self.collectionView endUpdates];
}