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 Datacache. 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, callrefresh().Declaration
Swift
public convenience init(sortDescriptors: [NSSortDescriptor], managedObjectContext context: NSManagedObjectContext)Parameters
sortDescriptorsProperties to sort the results, in order they are defined.
contextCore DataManaged Object Context. -
Inits the
Core Data Provider. The data will not be fetched after created. To fetch data, callrefresh().cacheName: Name for
Core Datacache.
Declaration
Swift
public init(sortDescriptors: [NSSortDescriptor], managedObjectContext context: NSManagedObjectContext, predicate: NSPredicate?, fetchLimit: NSInteger?, sectionNameKeyPath: String?, cacheName: String?)Parameters
sortDescriptorsProperties to sort the results, in order they are defined.
contextCore DataManaged Object Context.predicateCore Datapredicate used to filter results.fetchLimitUsed to limit the amount of returned entities.
sectionNameKeyPathA Key Path used for categorization and group data using the given
keypath. Note that, if defined it must be the first element in thesortDescriptors.
-
Fetchs data from
Core Datastorage. By default, it is not called by default when theCoreDataProvideris created.Also, this method must be called whenever
predicate,fetchLimitorsortDescriptorsis changed.Once executed,
objectswill 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 theCoreDataProvider.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
fetchRequestNSFetchRequest
used to request data. managedObjectContextCore Datacontext.sectionNameKeyPathkeypathfor section categorization.cacheNameCore Datacache name.
-
Returns the object of given
EntityTypeat givenindexPath, if exists.Declaration
Swift
override public subscript(indexPath: IndexPath) -> EntityType? { get }Parameters
indexPathIndexPath to get object.
-
Returns the IndexPath for the given object, if found.
Declaration
Swift
override public func path(for entity: EntityType) -> IndexPath?Parameters
entityObject to search.
Return Value
IndexPath.
-
Returns the numbers of provided sections.
For one-dimentional arrays, will return 1.
Declaration
Swift
override public func numberOfSections() -> IntReturn 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) -> IntParameters
sectionThe 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
sectionDesired section.
Return Value
String?
View on GitHub
CoreDataProvider Class Reference