Fetched results controller is an effective way for binding Core Data with UITableView or UICollectionView. As we all know both UITableView and UICollectionView is constructed with sections and rows for each of those section. And the fetched results controller also constructed in such a way with both sections and rows for each section which will match for the UITableView / UICollectionView data source and can be used for it.
Why its so effective ?
Lets list out why Fetched result controller is so effective when using CoreData,
- If we want to display core data content in a UITableView then we have to fetch respective contents of entities present in the core data and display it. We may get a case where we want to update the table view data when new values are added in core data. In this case if we want to load the newly inserted data in the UITableView then we want to fetch the core data contents again and reload the table view. But in this method we may not know when to perform the synchronize process again and again.
- To overcome this Core Data comes with the following solution.
- Whenever a record is inserted, deleted or updated in the Managed Object Context(MOC), It will post three types of notifications through notification center. They are,
Posted when new record is inserted, deleted or updated in managed object context.
Posted before the pending changes are commited to the backing store(database).
Posted after the changes are commited in the backing sore(database).
Note: The above notification contents consists of the records that was inserted, deleted or updated.
- Now lets come to our scenario, now we know that we will get the above notifications whenever there is some change in the coredata. So now we can make the synchronization process whenever any of the above notification is received.
- But there is an another way to handle this scenario, this is when Fetched results controller comes in, FRC is specifically suited for managing core data data with UITableView or UIColletionView and to do the above synchronization process by itself i.e., it takes care of all the synchronization process and updates the UI data whenever any change is made in core data and it does it in efficient manner by making use of the userInfo that the above listed notifications comes with.
Ok, now I hope this explains better about FRC’s effectiveness. Lets step into the process involved in configuring FRC.
How to configure FRC ?
The following steps are followed for configuring a Fetched results controller,
- Create an instance of NSFetchedResultsController.
@property (nonatomic, strong) NSFetchedResultsController *fetchedResultsController;
- Create a NSFetchRequest with an entity name in the database for which we need to construct fetched result controller for and
– (Mandatory) : The above fetcth request should contain atleast one sort descriptor which is used to sort the results.
– (Optional) : You can also set “setFetchBatchSize” value to specify the number of objects returned i.e, it splits into batches when the fetch request is executed. By default the batch size is 0 which is treated as infinite.
Initialize the fetch result controller instance. You do this by passing four parameters namely,
- The fetch request that we constructed above.
- Managed object context object.
- (Optional) : You could specify “sectionNameKeyPath” the fetch result controller uses this keypath to split the results into sections like in a tableview/collecitonview. If you pass nil for this argument then it will create all the results under a single section.
- (Optional) : You could specify “cacheName”, Using this cache can avoid the overhead of computing the section and index information. If you pass nil for this argument then it prevents caching.
self.fetchedResultsController = [[NSFetchedResultsController alloc] initWithFetchRequest:fetchRequest managedObjectContext:self.managedObjectContext sectionNameKeyPath:nil cacheName:nil];
- Configure the fetched result controller by setting its delegate as below,
- Perform Fetch action
- Now set the data for tableview/collectionview with fetchedresultcontroller instance as shown below(shown how to use in a tableview),
Implementing : UITableViewDataSource :-
Implementing the NSFetchedResultsControllerDelegate Protocols:
NSFetchedResultsController comes with few delegate methods which are used for denoting the status of updation of data in coredata, they are,
We can make use of these delegates for updating the tableView with respect to the coredatabase changes.
//Arguments for the above delegates are as follows
- An instance of NSFetchedResultsController.
- The changed/updated NSManagedObject instance.
- Current index path of object in fetched results controller.
- Change type : The changes are of many types namely Insert, Update, Delete or Move.
- New index path of the object in the fetched results controller.
Type of changes that can take place are as follows,
- NSFetchedResultsChangeUpdate &