CoreDataProvider

open class CoreDataProvider<EntityType> : ArrayDataProvider<EntityType>, NSFetchedResultsControllerDelegate, Refreshable where EntityType : NSManagedObject

A Core Data DataProvider having NSManagedObjects of type EntityType.

  • The predicate to be applied to every data fetch operation. After changing the entity, the data must be fetched again, execute refresh() method.

    Declaration

    Swift

    open var predicate: NSPredicate? { get set }
  • Limit to the amount of fetched objects. After changing the entity, the data must be fetched again, execute refresh() method.

    Declaration

    Swift

    open var fetchLimit: NSInteger? { get set }
  • Result sorting. After changing the entity, the data must be fetched again, execute refresh() method.

    Declaration

    Swift

    open var sortDescriptors: [NSSortDescriptor] { get set }
  • A Key Path used for categorization and group data using the given keypath. This entity cannot be changed.

    Declaration

    Swift

    private(set) open var sectionNameKeyPath: String?
  • Name for Core Data cache. This entity cannot be changed.

    Declaration

    Swift

    private(set) open var cacheName: String?
  • The Core Data Managed Object Context.

    Declaration

    Swift

    open let managedObjectContext: NSManagedObjectContext
  • Inits the Core Data Provider. The data will not be fetched after created. To fetch data, call refresh().

    Declaration

    Swift

    public convenience init(sortDescriptors: [NSSortDescriptor],
                            managedObjectContext context: NSManagedObjectContext)

    Parameters

    sortDescriptors

    Properties to sort the results, in order they are defined.

    context

    Core Data Managed Object Context.

  • Inits the Core Data Provider. The data will not be fetched after created. To fetch data, call refresh().

    • cacheName: Name for Core Data cache.

    Declaration

    Swift

    public init(sortDescriptors: [NSSortDescriptor],
                managedObjectContext context: NSManagedObjectContext,
                predicate: NSPredicate?,
                fetchLimit: NSInteger?,
                sectionNameKeyPath: String?,
                cacheName: String?)

    Parameters

    sortDescriptors

    Properties to sort the results, in order they are defined.

    context

    Core Data Managed Object Context.

    predicate

    Core Data predicate used to filter results.

    fetchLimit

    Used to limit the amount of returned entities.

    sectionNameKeyPath

    A Key Path used for categorization and group data using the given keypath. Note that, if defined it must be the first element in the sortDescriptors.

  • Fetchs data from Core Data storage. By default, it is not called by default when the CoreDataProvider is created.

    Also, this method must be called whenever predicate, fetchLimit or sortDescriptors is changed.

    Once executed, objects will return the fetched objects.

    If you do not want to handle any exception that may happen, you can use:

    try? coreDataProvider.refresh()
    

    If you want to handle any exception that may happen, you can use:

    do {
       try coreDataProvider.refresh()
    } catch let error as NSError {
       print("Error: \(error)")
    }
    

    Declaration

    Swift

    open func refresh() throws
  • The NSFetchedResultsController<EntityType> used to back the CoreDataProvider.

    Declaration

    Swift

    open lazy var fetchedResultsController: NSFetchedResultsController<EntityType> { get set }
  • Instantiates a new NSFetchedResultsController<EntityType>.

    Declaration

    Swift

    open func instantiateFetchedResultsController(_ fetchRequest: NSFetchRequest<EntityType>,
                                                  managedObjectContext: NSManagedObjectContext,
                                                  sectionNameKeyPath: String?,
                                                  cacheName: String?) -> NSFetchedResultsController<EntityType>

    Parameters

    fetchRequest

    NSFetchRequest used to request data.

    managedObjectContext

    Core Data context.

    sectionNameKeyPath

    keypath for section categorization.

    cacheName

    Core Data cache name.

  • Returns the object of given EntityType at given indexPath, if exists.

    Declaration

    Swift

    override public subscript(indexPath: IndexPath) -> EntityType? { get }

    Parameters

    indexPath

    IndexPath to get object.

  • Returns the IndexPath for the given object, if found.

    Declaration

    Swift

    override public func path(for entity: EntityType) -> IndexPath?

    Parameters

    entity

    Object to search.

    Return Value

    IndexPath.

  • Returns the numbers of provided sections.

    For one-dimentional arrays, will return 1.

    Declaration

    Swift

    override public func numberOfSections() -> Int

    Return Value

    Int.

  • Returns the number of objects in the given section.

    If given section does not exists, returns 0.

    Declaration

    Swift

    override public func numberOfItems(in section: Int) -> Int

    Parameters

    section

    The section to be inquired about how much provided objects it has.

    Return Value

    Int.

  • Returns the title for a given section.

    Declaration

    Swift

    override public func title(section: Int) -> String?

    Parameters

    section

    Desired section.

    Return Value

    String?