RelationalResultsController

public class RelationalResultsController<Section, Element> : FetchedResultsController<Section, Element> where Section : NSManagedObject, Element : NSManagedObject

Extending on FetchedResultsController and it’s section grouping, this controller allows for sections to be created from a parent ententy.-

In a FetchedResultsController (and NSFetchedResultsController) you would use sectionKeyPath to achieve the following:

Things
   { sectionKeyPath : "Things" }
   { sectionKeyPath : "Things" }

Not Things
   { sectionKeyPath : "Not Things" }
   { sectionKeyPath : "Not Things" }

While this is great, it does not work well for the common Parent-Child data model. In a Department - Employee model for example we woul want:

Sales {}
   Jim {}
   Samantha {}

Managment {}
   Sarah {}
   Howard {}


Delivery {}
   <No employees>

In this case, both the parent and child are NSManagedObjects joined by a relationship. Also, notice the Delivery department has no employees. With a standard FetchedResultsController where sections consist of the available values in the fetched objects, the Delivery would not be included. With a RelationalResultsController though you can opt to fetch both the sections and object independently (see fetchSections).

  • Intialize a controller

    Declaration

    Swift

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

    Parameters

    context

    A managed object context

    request

    A request for fetching objects

    sectionRequest

    A request for fetching sections (if fetchSections is true)

    keyPath

    The key path on the controllers objects that references the sections

  • Executes a fetch to populate the controller

    Declaration

    Swift

    override public func performFetch() throws
  • A fetch request used to fetch, filter, and sort the section results of the controller.

    This is used to validate the section objects. If fetchSections is true, section objects will be fetched independent of the child objects.

    A parent object that does not match the request here, may still be visible if it has children that match the predicate of fetchRequest.

    Declaration

    Swift

    public let sectionFetchRequest: NSFetchRequest<Section>
  • A keyPath of the section objects to get the displayable name

    For custom names, leave nil and conform your section objects to CustomDisplayStringConvertible

    Declaration

    Swift

    public var sectionNameKeyPath: KeyPath<Section, String>?
  • If true, sections will be fetched independent of objects using sectionFetchRequest.

    This is useful to populate the controller with section objects that may not have any children.

    Declaration

    Swift

    public var fetchSections: Bool