18.7. Boosting Data Access in Table Views
Problem
In an application that uses table views to present managed objects to the user, you want to be able to fetch and present the data in a more fluid and natural way than managing your data manually.
Solution
Use fetched results controllers, which are instances of
NSFetchedResultsController.
Note
In this recipe, we are going to use storyboards to reduce the time that it takes to develop the sample application in the Discussion section. For more information about storyboards, please refer to Chapter 6).
Discussion
Fetched results controllers work in the same way as table views. Both have sections and rows. A fetched results controller can read managed objects from a managed object context and separate them into sections and rows. Each section is a group (if you specify it), and each row in a section is a managed object. You can then easily map this data to a table view and display it to the user. There are a few very important reasons why you might want to modify your application to use fetched results controllers:
After a fetched results controller is created on a managed object context, any change (insertion, deletion, modification, etc.) will immediately be reflected on the fetched results controller as well. For instance, you could create your fetched results controller to read the managed objects of the
Personentity. Then in some other place in your application, you might insert a newPersonmanaged object into the context (the same context ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access