Last active
September 17, 2022 11:25
-
-
Save AppsTitude/ce072627c61ea3999b8d to your computer and use it in GitHub Desktop.
UICollectionView and NSFetchedResultsControllerDelegate integration in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// I just implemented that with Swift. So I would like to share my implementation. | |
// First initialise an array of NSBlockOperations: | |
var blockOperations: [NSBlockOperation] = [] | |
// In the did change object method: | |
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) { | |
if type == NSFetchedResultsChangeType.Insert { | |
println("Insert Object: \(newIndexPath)") | |
blockOperations.append( | |
NSBlockOperation(block: { [weak self] in | |
if let this = self { | |
this.collectionView!.insertItemsAtIndexPaths([newIndexPath!]) | |
} | |
}) | |
) | |
} | |
else if type == NSFetchedResultsChangeType.Update { | |
println("Update Object: \(indexPath)") | |
blockOperations.append( | |
NSBlockOperation(block: { [weak self] in | |
if let this = self { | |
this.collectionView!.reloadItemsAtIndexPaths([indexPath!]) | |
} | |
}) | |
) | |
} | |
else if type == NSFetchedResultsChangeType.Move { | |
println("Move Object: \(indexPath)") | |
blockOperations.append( | |
NSBlockOperation(block: { [weak self] in | |
if let this = self { | |
this.collectionView!.moveItemAtIndexPath(indexPath!, toIndexPath: newIndexPath!) | |
} | |
}) | |
) | |
} | |
else if type == NSFetchedResultsChangeType.Delete { | |
println("Delete Object: \(indexPath)") | |
blockOperations.append( | |
NSBlockOperation(block: { [weak self] in | |
if let this = self { | |
this.collectionView!.deleteItemsAtIndexPaths([indexPath!]) | |
} | |
}) | |
) | |
} | |
} | |
// In the did change section method: | |
func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) { | |
if type == NSFetchedResultsChangeType.Insert { | |
println("Insert Section: \(sectionIndex)") | |
blockOperations.append( | |
NSBlockOperation(block: { [weak self] in | |
if let this = self { | |
this.collectionView!.insertSections(NSIndexSet(index: sectionIndex)) | |
} | |
}) | |
) | |
} | |
else if type == NSFetchedResultsChangeType.Update { | |
println("Update Section: \(sectionIndex)") | |
blockOperations.append( | |
NSBlockOperation(block: { [weak self] in | |
if let this = self { | |
this.collectionView!.reloadSections(NSIndexSet(index: sectionIndex)) | |
} | |
}) | |
) | |
} | |
else if type == NSFetchedResultsChangeType.Delete { | |
println("Delete Section: \(sectionIndex)") | |
blockOperations.append( | |
NSBlockOperation(block: { [weak self] in | |
if let this = self { | |
this.collectionView!.deleteSections(NSIndexSet(index: sectionIndex)) | |
} | |
}) | |
) | |
} | |
} | |
// And finally, in the did controller did change content method: | |
func controllerDidChangeContent(controller: NSFetchedResultsController) { | |
collectionView!.performBatchUpdates({ () -> Void in | |
for operation: NSBlockOperation in self.blockOperations { | |
operation.start() | |
} | |
}, completion: { (finished) -> Void in | |
self.blockOperations.removeAll(keepCapacity: false) | |
}) | |
} | |
// I personally added some code in the deinit method as well, in order to cancel the operations when the ViewController is about to get deallocated: | |
deinit { | |
// Cancel all block operations when VC deallocates | |
for operation: NSBlockOperation in blockOperations { | |
operation.cancel() | |
} | |
blockOperations.removeAll(keepCapacity: false) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hey all, just converted the above to swift 3 : https://gist.github.com/nazywamsiepawel/e88790a1af1935ff5791c9fe2ea19675