Skip to content

Instantly share code, notes, and snippets.

@iwasrobbed
Last active June 5, 2020 20:34
Show Gist options
  • Select an option

  • Save iwasrobbed/5528897 to your computer and use it in GitHub Desktop.

Select an option

Save iwasrobbed/5528897 to your computer and use it in GitHub Desktop.

Revisions

  1. rob phillips revised this gist Feb 8, 2014. 1 changed file with 14 additions and 14 deletions.
    28 changes: 14 additions & 14 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
    {
    shouldReloadCollectionView = NO;
    blockOperation = [NSBlockOperation new];
    self.shouldReloadCollectionView = NO;
    self.blockOperation = [[NSBlockOperation alloc] init];
    }

    - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo
    @@ -10,21 +10,21 @@ - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id
    __weak UICollectionView *collectionView = self.collectionView;
    switch (type) {
    case NSFetchedResultsChangeInsert: {
    [blockOperation addExecutionBlock:^{
    [self.blockOperation addExecutionBlock:^{
    [collectionView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
    }];
    break;
    }

    case NSFetchedResultsChangeDelete: {
    [blockOperation addExecutionBlock:^{
    [self.blockOperation addExecutionBlock:^{
    [collectionView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
    }];
    break;
    }

    case NSFetchedResultsChangeUpdate: {
    [blockOperation addExecutionBlock:^{
    [self.blockOperation addExecutionBlock:^{
    [collectionView reloadSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
    }];
    break;
    @@ -42,38 +42,38 @@ - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)
    case NSFetchedResultsChangeInsert: {
    if ([self.collectionView numberOfSections] > 0) {
    if ([self.collectionView numberOfItemsInSection:indexPath.section] == 0) {
    shouldReloadCollectionView = YES;
    self.shouldReloadCollectionView = YES;
    } else {
    [blockOperation addExecutionBlock:^{
    [self.blockOperation addExecutionBlock:^{
    [collectionView insertItemsAtIndexPaths:@[newIndexPath]];
    }];
    }
    } else {
    shouldReloadCollectionView = YES;
    self.shouldReloadCollectionView = YES;
    }
    break;
    }

    case NSFetchedResultsChangeDelete: {
    if ([self.collectionView numberOfItemsInSection:indexPath.section] == 1) {
    shouldReloadCollectionView = YES;
    self.shouldReloadCollectionView = YES;
    } else {
    [blockOperation addExecutionBlock:^{
    [self.blockOperation addExecutionBlock:^{
    [collectionView deleteItemsAtIndexPaths:@[indexPath]];
    }];
    }
    break;
    }

    case NSFetchedResultsChangeUpdate: {
    [blockOperation addExecutionBlock:^{
    [self.blockOperation addExecutionBlock:^{
    [collectionView reloadItemsAtIndexPaths:@[indexPath]];
    }];
    break;
    }

    case NSFetchedResultsChangeMove: {
    [blockOperation addExecutionBlock:^{
    [self.blockOperation addExecutionBlock:^{
    [collectionView moveItemAtIndexPath:indexPath toIndexPath:newIndexPath];
    }];
    break;
    @@ -87,11 +87,11 @@ - (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)
    - (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
    {
    // Checks if we should reload the collection view to fix a bug @ http://openradar.appspot.com/12954582
    if (shouldReloadCollectionView) {
    if (self.shouldReloadCollectionView) {
    [self.collectionView reloadData];
    } else {
    [self.collectionView performBatchUpdates:^{
    [blockOperation start];
    [self.blockOperation start];
    } completion:nil];
    }
    }
  2. iwasrobbed created this gist May 6, 2013.
    97 changes: 97 additions & 0 deletions gistfile1.m
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,97 @@
    - (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
    {
    shouldReloadCollectionView = NO;
    blockOperation = [NSBlockOperation new];
    }

    - (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id<NSFetchedResultsSectionInfo>)sectionInfo
    atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type
    {
    __weak UICollectionView *collectionView = self.collectionView;
    switch (type) {
    case NSFetchedResultsChangeInsert: {
    [blockOperation addExecutionBlock:^{
    [collectionView insertSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
    }];
    break;
    }

    case NSFetchedResultsChangeDelete: {
    [blockOperation addExecutionBlock:^{
    [collectionView deleteSections:[NSIndexSet indexSetWithIndex:sectionIndex]];
    }];
    break;
    }

    case NSFetchedResultsChangeUpdate: {
    [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] > 0) {
    if ([self.collectionView numberOfItemsInSection:indexPath.section] == 0) {
    shouldReloadCollectionView = YES;
    } else {
    [blockOperation addExecutionBlock:^{
    [collectionView insertItemsAtIndexPaths:@[newIndexPath]];
    }];
    }
    } else {
    shouldReloadCollectionView = YES;
    }
    break;
    }

    case NSFetchedResultsChangeDelete: {
    if ([self.collectionView numberOfItemsInSection:indexPath.section] == 1) {
    shouldReloadCollectionView = YES;
    } else {
    [blockOperation addExecutionBlock:^{
    [collectionView deleteItemsAtIndexPaths:@[indexPath]];
    }];
    }
    break;
    }

    case NSFetchedResultsChangeUpdate: {
    [blockOperation addExecutionBlock:^{
    [collectionView reloadItemsAtIndexPaths:@[indexPath]];
    }];
    break;
    }

    case NSFetchedResultsChangeMove: {
    [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 (shouldReloadCollectionView) {
    [self.collectionView reloadData];
    } else {
    [self.collectionView performBatchUpdates:^{
    [blockOperation start];
    } completion:nil];
    }
    }