secret
Last active

UICollectionView + NSfetchedResultsControllerDelegate

  • Download Gist
gistfile1.m
Objective-C
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
/*
The MIT License (MIT)
 
Copyright (c) 2013 Lucien Constantino
 
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.
*/
 
- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath
{
NSMutableDictionary *change = [NSMutableDictionary new];
switch(type)
{
case NSFetchedResultsChangeInsert:
change[@(type)] = newIndexPath;
break;
case NSFetchedResultsChangeDelete:
change[@(type)] = indexPath;
break;
case NSFetchedResultsChangeUpdate:
change[@(type)] = indexPath;
break;
case NSFetchedResultsChangeMove:
change[@(type)] = @[indexPath, newIndexPath];
break;
}
[_objectChanges addObject:change];
}
 
- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
NSLog(@"section changes: %i", [_sectionChanges count]);
NSLog(@"obj changes: %i", [_objectChanges count]);
if ([_sectionChanges count] > 0)
{
[_collectionView performBatchUpdates:^{
for (NSDictionary *change in _sectionChanges)
{
[change enumerateKeysAndObjectsUsingBlock:^(NSNumber *key, id obj, BOOL *stop) {
NSFetchedResultsChangeType type = [key unsignedIntegerValue];
switch (type)
{
case NSFetchedResultsChangeInsert:
[self.collectionView insertSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];
break;
case NSFetchedResultsChangeDelete:
[self.collectionView deleteSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];
break;
case NSFetchedResultsChangeUpdate:
[self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:[obj unsignedIntegerValue]]];
break;
}
}];
}
} completion:nil];
}
if ([_objectChanges count] > 0 && [_sectionChanges count] == 0)
{
[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:^(BOOL finished) {
}];
}
[_sectionChanges removeAllObjects];
[_objectChanges removeAllObjects];
}

added Objective-C Literals

Have you thought about writing similar code for the NSFetchedResultsControllerDelegate methods regarding section changes?

  • (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id )sectionInfo
    atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
    NSMutableDictionary *change = [NSMutableDictionary new];

    switch(type) {
    case NSFetchedResultsChangeInsert:
    change[@(type)] = @[sectionIndex];
    break;
    case NSFetchedResultsChangeDelete:
    change[@(type)] = @[sectionIndex];
    break;
    }

[_sectionChanges addObject:change];
}

And so on...

Yes I've written and will update soon.

Computers are like a bicycle for our minds.  Steve Jobs

On 04/08/2012, at 18:39, DBraun
reply@reply.github.com
wrote:

Have you thought about writing similar code for the NSFetchedResultsControllerDelegate methods regarding section changes?

  • (void)controller:(NSFetchedResultsController *)controller didChangeSection:(id )sectionInfo
    atIndex:(NSUInteger)sectionIndex forChangeType:(NSFetchedResultsChangeType)type {
    NSMutableDictionary *change = [NSMutableDictionary new];

    switch(type) {
    case NSFetchedResultsChangeInsert:
    change[@(type)] = @[sectionIndex];
    break;
    case NSFetchedResultsChangeDelete:
    change[@(type)] = @[sectionIndex];
    break;
    }

[_sectionChanges addObject:change];
}

And so on...


Reply to this email directly or view it on GitHub:
https://gist.github.com/4440c1cba83318e276bb

The only problem I see here is that controller:didChangeSection:atIndex:forChangeType: does not notifies for a sections moved and you can do this with a section in UICollectionView

Just a question...is this category on UICollectionViewController and not UICollectionView?

You have several references to self.collectionView / _collectionView

(thanks for taking the time for this!)

Found this via https://github.com/AshFurrow/UICollectionView-NSFetchedResultsController subsequently forked and refactored the logic out into a category on UICollectionView. Available here: https://github.com/derrh/UICollectionView-NSFetchedResultsController

Thanks, Lucien, for the code.

FYI, you can do this in a much simpler fashion by making use of NSBlockOperation. Observe:

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller
{
    self.blockOperation = [NSBlockOperation new];
}

- (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: {
            [self.blockOperation addExecutionBlock:^{
                [collectionView insertSections:[NSIndexSet indexSetWithIndex:newIndexPath.section] ];
            }];
            break;
        }

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

        case NSFetchedResultsChangeUpdate: {
            [self.blockOperation addExecutionBlock:^{
                [collectionView reloadSections:[NSIndexSet indexSetWithIndex:indexPath.section]];
            }];
            break;
        }

        case NSFetchedResultsChangeMove: {
            [self.blockOperation addExecutionBlock:^{
                [collectionView moveSection:indexPath.section toSection:newIndexPath.section];
            }];
            break;
        }

        default:
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller
{
    [self.collectionView performBatchUpdates:^{
        [self.blockOperation start];
    } completion:^(BOOL finished) {
        // Do whatever
    }];
}

Hi Blake,

Indeed, it seems to be a much cleaner implementation, but why are you trying to update the whole section instead just a item? something like this:

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


    UICollectionView * __weak collectionView = self.collectionView;
    switch (type) {
        case NSFetchedResultsChangeInsert: {
            [self.collectionReloadOperation addExecutionBlock:^{
                [collectionView insertItemsAtIndexPaths:@[newIndexPath]];
            }];
            break;
        }case NSFetchedResultsChangeDelete: {
            [self.collectionReloadOperation addExecutionBlock:^{
                [collectionView deleteItemsAtIndexPaths:@[indexPath]];
            }];
            break;
        }case NSFetchedResultsChangeUpdate: {
            [self.collectionReloadOperation addExecutionBlock:^{
                [collectionView reloadItemsAtIndexPaths:@[indexPath]];
            }];
            break;
        }case NSFetchedResultsChangeMove: {
            [self.collectionReloadOperation addExecutionBlock:^{
                [collectionView moveItemAtIndexPath:indexPath toIndexPath:newIndexPath];
            }];
            break;
        }default:
            break;
    }
}

Also, the documentation for NSBlockOperation (about addExecutionBlock ) says:

The specified block should not make any assumptions about its execution environment.

Do you think it's ok to execute that operation inside performBatchUpdates?

Thanks,
StaS

Blake's approach is indeed pretty elegant, however anyone using it should be aware that NSBlockOperation doesn't give any guarantees on the block execution context (used thread). Since we're dealing with UIKit here, it's crucial to make sure that the blocks are executed on the main thread, a requirement NSBlockOperation fails to satisfy (simply verifiable by inserting assert([NSThread isMainThread]); inside the blocks).

Here's a simple solution that should be much safer, and is just as straightforward.

- (void)controllerWillChangeContent:(NSFetchedResultsController *)controller {
    self.updateBlocks = [NSMutableArray new];
}

- (void)controller:(NSFetchedResultsController *)controller didChangeObject:(id)anObject atIndexPath:(NSIndexPath *)indexPath forChangeType:(NSFetchedResultsChangeType)type newIndexPath:(NSIndexPath *)newIndexPath {
    __weak UICollectionView *collectionView = self.galleryView;
    switch (type) {
        case NSFetchedResultsChangeInsert: {
            [self.updateBlocks addObject:^{
                [collectionView insertItemsAtIndexPaths:@[newIndexPath]];
            }];
            break;
        }
        case NSFetchedResultsChangeDelete: {
            [self.updateBlocks addObject:^{
                [collectionView deleteItemsAtIndexPaths:@[indexPath]];
            }];
            break;
        }
        case NSFetchedResultsChangeUpdate: {
            [self.updateBlocks addObject:^{
                [collectionView reloadItemsAtIndexPaths:@[indexPath]];
            }];
            break;
        }
        case NSFetchedResultsChangeMove: {
            [self.updateBlocks addObject:^{
                [collectionView moveItemAtIndexPath:indexPath toIndexPath:newIndexPath];
            }];
            break;
        }
        default:
            break;
    }
}

- (void)controllerDidChangeContent:(NSFetchedResultsController *)controller {
    [self.galleryView performBatchUpdates:^{
        for (void (^updateBlock)(void) in self.updateBlocks) {
            updateBlock();
        }
    } completion:nil];
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.