UITableViewController and NSFetchedResultsController

NSFetchedResultsController takes a lot of the work out of managing the display of data returned from a Core Data fetch request. The basic mechanism is to create a fetch request detailing what you are looking for, how to filter it, and how to order the results. This is then attached to a new NSFetchedResultsController, which queries Core Data. The magic of the NSFetchedResultsController is that it can monitor objects stored in Core Data and automatically change the table view to reflect changes to these objects. Once set up you just make  changes to  your Core Data objects, and leave NSFetchedResultsController to adjust the table accordingly. Even better is that Apple have already written a whole bunch of code to help  with the initial setup.

After all that, lets begin to build an app. First, open Xcode and create a new ‘Single-View Application’ project called MagicNotes.
New MagicNotes project

Create a TableViewController That Understands a NSFetchedResultsController

Next, create a new Objective-C class (File>New>Cocoa Touch>Objective-C Class) called CoreDataTableViewController, which is a subclass of UITableViewController.  Copy in the following code to the header and implementation files (collated from the Apple Documentation).

For more complicated apps where table rows can be moved you need to consider suspending tracking of changes during certain user operations. For more info check out the Stanford CS193p course material (in fact do it anyway, as it’s a great course). I’m not using (as many do) the CoreDataTableViewController code from that course, as it doesn’t have a commercial license.

So what does all this code do? First it defines some basic functionality: fetching data, reloading the table view, and handling the creation of new NSFetchedResultControllers. Next it defines all the methods necessary to convert results into the specifications for the table view (e.g. number of table sections, number of rows in each section). Finally, it covers the NSFetchedResultsControllerDelegate protocol. On a basic level this monitors for changes in the data, and makes the necessary changes to the table view.

Subclass Your Clever CoreDataTableViewController

The great things about the CoreDataTableViewController is that it’s highly reusable. We just need to subclass it and add a couple of methods so lets do just that.  Create a new Objective-C class (File>New>Cocoa Touch>Objective-C Class)  called NotesListViewController that is a subclass of CoreDataTableViewController. For now, we just need to add 2 methods.

and add one line to viewDidLoad

 Build the Storyboard

We will now start to build the (very basic) storyboard. Open up one of the storyboards (the choice doesn’t matter – just make sure you test on the same device or make all changes to both storyboards). Delete the view controller that’s already provided, drop a TableViewController onto the storyboard, change its Custom Class to ‘NotesListViewController’, and embed it in a Navigation Controller (Editor>Embed In>Navigation Controller). Click on the table cell, change the style to “Subtitle” and give is the Reuse Identifier “Note Cell”.

Basic MagicNote Storyboard

That is everything for this part. At this point you can click run and check you have no errors. If not you will see a very boring app with a blank table. In the next part, we will start to populate that table with some dummy data taken from our Core Data storage.

Series Navigation<< Magical Record and NSFetchedResultsController: Core Data made easyMagical Record: Simple Core Data >>

Leave a Reply

  • (will not be published)

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">