FetchedResultsController

public class FetchedResultsController<Section, Element> : MutableResultsController<Section, Element> where Section : SectionType, Element : NSManagedObject

A FetchedResultsController provides the same data store and change reporting as a MutableResultsController but sources it’s contents from a CoreData context.

Given an NSFetchRequest, the results from the provided context are fetched and analyzed to provide the data necessary to populate a CollectionView.

The controller can also be sorted, grouped into sections and automatically updated when changes are made in the managed obejct context.

  • Controller initializer a given context and fetch request

    Declaration

    Swift

    public init(context: NSManagedObjectContext, request: NSFetchRequest<Element>, sectionKeyPath: KeyPath<Element, Section>? = nil)

    Parameters

    context

    A managed object context

    request

    A fetch request with an entity name

    sectionKeyPath

    An optional key path to use for section groupings

  • An object the report to when content in the controller changes

    Declaration

    Swift

    public override weak var delegate: ResultsControllerDelegate? { get set }
  • The fetch request for the controller

    Declaration

    Swift

    public let fetchRequest: NSFetchRequest<Element>
  • The managed object context to fetch from

    Declaration

    Swift

    public var managedObjectContext: NSManagedObjectContext { get }
  • Update the managed object context used by the controller

    Declaration

    Swift

    public func setManagedObjectContext(_ moc: NSManagedObjectContext) throws

    Parameters

    moc

    The new context to use

    Return Value

    This implicitly calls performFetch which can throw

  • Performs the provided fetch request to populate the controller. Calling again resets the controller.

    Throws

    If the fetch request is invalid or the fetch fails

    Declaration

    Swift

    public func performFetch() throws
  • Clears all data and stops monitoring for changes in the context.

    Declaration

    Swift

    public override func reset()