Skip to content

Instantly share code, notes, and snippets.

@tempire
Created August 30, 2015 10:31
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tempire/debbabb2cccafa90320e to your computer and use it in GitHub Desktop.
Save tempire/debbabb2cccafa90320e to your computer and use it in GitHub Desktop.
collectionview begin/end coredata updates
import Foundation
import UIKit
import CoreData
class PresentationsListVC: UIViewController {
@IBOutlet weak var collectionView: UICollectionView?
// UICollectionView doesn't do beginUpdates/endUpdates.
// CoreData modifications are stored in here and performed together
var collectionViewItemChanges = [CollectionViewChange]()
var fetchedResultsController: NSFetchedResultsController?
var model: Model? { didSet { initializeFetchedResultsController() } }
override func viewDidAppear(animated: Bool) {
tagScreen(_stdlib_getDemangledTypeName(self))
customizeViewAppearance()
}
func customizeViewAppearance() {
}
}
// MARK: NSFetchedResultsController and Delegate
extension PresentationsListVC: NSFetchedResultsControllerDelegate {
func initializeFetchedResultsController() {
if let model = model {
let controller = model.fetchedResultsController("Presentation",
sortDescriptors: [NSSortDescriptor(key: "createdDate", ascending: false)]
)
var error: NSError?
if !controller.performFetch(&error) {
println("*** ERROR: Cannot perform fetch \(controller) \(error)")
}
controller.delegate = self
fetchedResultsController = controller
collectionView?.reloadData()
}
}
func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) {
collectionViewItemChanges.append([type: [indexPath, newIndexPath]])
}
func controllerDidChangeContent(controller: NSFetchedResultsController) {
collectionView?.performBatchUpdates({ self.collectionViewItemChanges.map {self.performCollectionViewChange($0)} }, completion: nil)
}
func performCollectionViewChange(change: CollectionViewChange) {
let type = change.keys.array.first!
let indexPaths = change[type] as! [NSIndexPath]
switch change.keys.array.first! {
case .Insert:
collectionView?.insertItemsAtIndexPaths(indexPaths)
case .Update:
collectionView?.reloadItemsAtIndexPaths(indexPaths)
case .Move:
collectionView?.moveItemAtIndexPath(indexPaths[0], toIndexPath: indexPaths[1])
case .Delete:
collectionView?.deleteItemsAtIndexPaths(indexPaths)
}
}
}
// MARK: Collection view delegate
extension PresentationsListVC: UICollectionViewDelegate, UICollectionViewDataSource {
func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
return 1
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return fetchedResultsController?.sections?[section].numberOfObjects ?? 0
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if let item = fetchedResultsController?.objectAtIndexPath(indexPath) as? Presentation {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier("PresentationsVCCell", forIndexPath: indexPath) as! UICollectionViewCell
let imageView = UIImageView(image: UIImage(named: "login-logo"))
cell.contentView.addSubview(imageView)
return cell
}
return UICollectionViewCell()
}
}
@ankits16
Copy link

ankits16 commented Mar 8, 2017

Please provide declaration for CollectionViewChange

@MadavaramRamesh
Copy link

I am not getting what is declaration for CollectionViewChange( in line 11). Please provide details for CollectionViewChange

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment