Created
August 30, 2015 10:31
-
-
Save tempire/debbabb2cccafa90320e to your computer and use it in GitHub Desktop.
collectionview begin/end coredata updates
This file contains hidden or 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
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() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I am not getting what is declaration for CollectionViewChange( in line 11). Please provide details for CollectionViewChange