CoreDataCollectionViewController
public class CoreDataCollectionViewController: UICollectionViewController, NSFetchedResultsControllerDelegate
Same concept as CoreDataTableViewController, but modified for use with UICollectionViewController.
This class mostly just copies the code from NSFetchedResultsController documentation page
into a subclass of UICollectionViewController.
Just subclass this and set the fetchedResultsController.
The only UICollectionViewDataSource method you’ll HAVE to implement is collectionView:cellForItemAtIndexPath:.
And you can use the NSFetchedResultsController method objectAtIndexPath: to do it.
Remember that once you create an NSFetchedResultsController, you CANNOT modify its properties.
If you want new fetch parameters (predicate, sorting, etc.),
create a NEW NSFetchedResultsController and set this class’s fetchedResultsController property again.
-
The controller (this class fetches nothing if this is not set).
Declaration
Swift
public var fetchedResultsController: NSFetchedResultsController? -
Causes the
fetchedResultsControllerto refetch the data. You almost certainly never need to call this. TheNSFetchedResultsControllerclass observes the context (so if the objects in the context change, you do not need to callperformFetchsince theNSFetchedResultsControllerwill notice and update the collection view automatically). This will also automatically be called if you change thefetchedResultsControllerproperty.Declaration
Swift
public func performFetch() throws -
Turn this on before making any changes in the managed object context that are a one-for-one result of the user manipulating cells directly in the collection view. Such changes cause the context to report them (after a brief delay), and normally our
fetchedResultsControllerwould then try to update the collection view, but that is unnecessary because the changes were made in the collection view already (by the user) so thefetchedResultsControllerhas nothing to do and needs to ignore those reports. Turn this back off after the user has finished the change. Note that the effect of setting this to NO actually gets delayed slightly so as to ignore previously-posted, but not-yet-processed context-changed notifications, therefore it is fine to set this to YES at the beginning of, e.g.,collectionView:moveItemAtIndexPath:toIndexPath:, and then set it back to NO at the end of your implementation of that method. It is not necessary (in fact, not desirable) to set this during row deletion or insertion (but definitely for cell moves).Declaration
Swift
public var suspendAutomaticTrackingOfChangesInManagedObjectContext: Bool
-
Notifies the receiver of the addition or removal of a section.
:param: controller The fetched results controller that sent the message. :param: sectionInfo The section that changed. :param: sectionIndex The index of the changed section. :param: type The type of change (insert or delete).
Declaration
Swift
public func controller(controller: NSFetchedResultsController, didChangeSection sectionInfo: NSFetchedResultsSectionInfo, atIndex sectionIndex: Int, forChangeType type: NSFetchedResultsChangeType) -
Notifies the receiver that a fetched object has been changed due to an add, remove, move, or update.
:param: controller The fetched results controller that sent the message. :param: anObject The object in controller’s fetched results that changed. :param: indexPath The index path of the changed object (this value is nil for insertions). :param: type The type of change. :param: newIndexPath The destination path for the object for insertions or moves (this value is nil for a deletion).
Declaration
Swift
public func controller(controller: NSFetchedResultsController, didChangeObject anObject: AnyObject, atIndexPath indexPath: NSIndexPath?, forChangeType type: NSFetchedResultsChangeType, newIndexPath: NSIndexPath?) -
Notifies the receiver that the fetched results controller has completed processing of one or more changes due to an add, remove, move, or update.
:param: controller The fetched results controller that sent the message.
Declaration
Swift
public func controllerDidChangeContent(controller: NSFetchedResultsController)
-
Asks the data source for the number of sections in the collection view.
:param: collectionView An object representing the collection view requesting this information.
:returns: The number of sections in collectionView.
Declaration
Swift
override public func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int -
Asks the data source for the number of items in the specified section. (required)
:param: collectionView An object representing the collection view requesting this information. :param: section An index number identifying a section in collectionView.
:returns: The number of rows in section.
Declaration
Swift
override public func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
View on GitHub
CoreDataCollectionViewController Class Reference