Swift — 4 — Core Data — Part 6 Using NSFetchResultController with UITableView

Hello, hope you are doing good. In this part we will discuss about NSFetchResultController (NSFRC). I will suggest you to read previous tutorials on core data also:

Basics

Simple App

Core data singleton

Unit testing with core data

Core data migration

Please support me by comment, like and share my facebook page.

NSFetchResultController

  • We can use NSFetchResultController when we need fetching, inserting, updating and deleting in core data and we need to update user interface like UITableView and UICollectionView.
  • Every time a insert, update or delete for a managed object model is performed in managed object context, NSFRC provides the delegate call backs.
  • NSFRC provides a method performFetch() which returns array of NSManagedObject models. This array of NSManagedObject models works very well with UITableViewDelegate and UITableViewDataSource methods as a feed array.
  • NSFRC only works with core data  ❗

Please download starter project from here

Have a ☕ and start code –

A basic initialization of a FetchResultController contains below steps:

  • NSManagedObjectContext – A managed object context, NSManagedObjectContext
  • NSFetchRequest – A fetch request containing information about what entity to fetch from core data store.
  • NSSortDescriptor –  A sort descriptor for arranging result in ascending or descending order.
  • An optional section name to be displayed in table view section.
  • Cache name, If you have a read only data set, then NSFetchResultController will fetch/read data from store at the first time. The next fetches will use cache to get data automatically.
  • A NSFetchedResultsController class obviously.
  • A delegate which is responsible for getting call backs of core data operations like insert, update, delete.

Initialization

Keeping all these points in mind lets open the starter project and copy paste below code in CoreDataManager class:

Why we have used lazy var here?

This is Lazy initialization of NSFetchedResultsController classLazy initialization is the tactic of delaying the creation of an object, the calculation of a value, or some other expensive tasks like initializing below objects

  • A managed object context, NSManagedObjectContext
  • A fetch request, NSFetchRequest
  • A sort descriptor, NSSortDescriptor

until the first time NSFetchResultController is needed.

Implementing Delegate

Also, to get complete benefits of NSFRC we should also implement delegate methods of NSFetchedResultsControllerDelegate protocol in ViewCotroller class. Copy paste below code in ViewController class:

Actually we are almost done  😀 .

Now we need to make some changes in ViewController class. Let’s do one by one:

  • Comment out people array:

We comment out people array because fetchResultsController will provide us number of rows, sections and objects at particular index paths. Doesn’t it’s a great!

  • Update save function as below:

  • Update delete(ssn:) function as below:

  • Update fetchAllPersons function as below:

  • Update delete(person:) function as below:

  • Update update(name:, ssn :, person 🙂 function as below:

  • Update numberOfRowsInSection as below:

  • Update cellForRow delegate and add configureCell method as below:

Finally your ViewController class should look like this:

Whenever a managed object context’s save is called it triggers NSFetchResultController’s delegate method. And also there is a defined order in which these delegate method will be called:

At this point if we run project we should be able to insert, update, delete and fetch records.

Hope you will be able to use it now. In next part we will implement section and cache  of NSFetchResultController.

Please support me by comment, like and share my facebook page.

This the the final source code.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.