Database Reference
In-Depth Information
Chapter 5:
NSFetchedResultsController
By Pietro Rea
If you followed the previous chapters closely, you probably noticed that most of the
sample projects use table views. That's because Core Data fits nicely with table
views.
Set up your fetch request, fetch an array of managed objects and plug the result
into the table view's data source. This is a common, everyday scenario.
If you see a tight relationship between Core Data and UITableView , you're in good
company. The authors of the Core Data framework at Apple thought the same way!
In fact, they saw so much potential for a close connection between UITableView and
Core Data that they penned a class to formalize this bond:
NSFetchedResultsController .
As the name suggests, NSFetchedResultsController is a controller, but it is not a
view controller. It has no user interface. Its purpose is to make developers' lives
easier by abstracting away much of the code needed to synchronize a table view
with a data source backed by Core Data.
Set up an NSFetchedResultsController correctly, and your table will “magically”
mimic its data source without you have to write more than a few lines of code. In
this chapter, you'll learn the ins and outs of this class. You'll also learn when to use
it and when not to use it. Are you ready?
Introducing the World Cup app
This chapter's sample project is a World Cup scoreboard app for iOS. A very simple
one! On startup, the one-page application will list all the teams contesting for the
World Cup. Tapping on a country's cell will increase the country's wins by one. In
this simplified version of the World Cup, the country with the most taps wins the
tournament. This ranking simplifies the real elimination rules quite a bit, but it's
good enough for demonstration purposes.
Go to this chapter's files and find the WorldCup-Starter folder. Open
WorldCup.xcodeproj . Build and run the starter project:
 
 
Search WWH ::




Custom Search