NSFetchedResultsContollerDelegate for CollectionView

前端 未结 6 1081
悲哀的现实
悲哀的现实 2020-11-30 21:10

I\'d like to use the NSFetchedResultsControllerRelegate in a CollectionViewController. Therefore I just changed the method for the TableViewController for the CollectionView

6条回答
  •  长情又很酷
    2020-11-30 21:27

    Here's a bit of Swift that works with UICollectionViewController's installsStandardGestureForInteractiveMovement and is a somewhat DRYed up and switches on the installsStandardGestureForInteractiveMovement so that all the code paths are obvious. It's the same overall pattern as Plot's code.

    var fetchedResultsProcessingOperations: [NSBlockOperation] = []
    
    private func addFetchedResultsProcessingBlock(processingBlock:(Void)->Void) {
        fetchedResultsProcessingOperations.append(NSBlockOperation(block: processingBlock))
    }
    
    func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
    
        switch type {
        case .Insert:
            addFetchedResultsProcessingBlock {self.collectionView!.insertItemsAtIndexPaths([newIndexPath!])}
        case .Update:
            addFetchedResultsProcessingBlock {self.collectionView!.reloadItemsAtIndexPaths([indexPath!])}
        case .Move:
            addFetchedResultsProcessingBlock {
                // If installsStandardGestureForInteractiveMovement is on
                // the UICollectionViewController will handle this on its own.
                guard !self.installsStandardGestureForInteractiveMovement else {
                    return
                }
                self.collectionView!.moveItemAtIndexPath(indexPath!, toIndexPath: newIndexPath!)
            }
        case .Delete:
            addFetchedResultsProcessingBlock {self.collectionView!.deleteItemsAtIndexPaths([indexPath!])}
        }
    
    }
    
    func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) {
    
        switch type {
        case .Insert:
            addFetchedResultsProcessingBlock {self.collectionView!.insertSections(NSIndexSet(index: sectionIndex))}
        case .Update:
            addFetchedResultsProcessingBlock {self.collectionView!.reloadSections(NSIndexSet(index: sectionIndex))}
        case .Delete:
            addFetchedResultsProcessingBlock {self.collectionView!.deleteSections(NSIndexSet(index: sectionIndex))}
        case .Move:
            // Not something I'm worrying about right now.
            break
        }
    
    }
    
    func controllerDidChangeContent(controller: NSFetchedResultsController) {
        collectionView!.performBatchUpdates({ () -> Void in
            for operation in self.fetchedResultsProcessingOperations {
                operation.start()
            }
            }, completion: { (finished) -> Void in
                self.fetchedResultsProcessingOperations.removeAll(keepCapacity: false)
        })
    }
    
    deinit {
        for operation in fetchedResultsProcessingOperations {
            operation.cancel()
        }
    
        fetchedResultsProcessingOperations.removeAll()
    }
    

提交回复
热议问题