Database Reference
In-Depth Information
In addition to incrementing a team's number of wins, tapping a cell now reloads the
entire table view. This approach is heavy-handed but it does the job for now.
Build and run the app one more time. Tap as many countries as you want as many
times as you want. Verify that the UI is always up to date.
There you go. You've got a fetched results controller up and running. Excited?
If this were all
NSFetchedResultsController
could do, you would probably feel a
little disappointed. After all, you can accomplish the same thing using an
NSFetchRequest
and a simple array.
The real magic comes in the remaining sections of this chapter.
NSFetchedResultsController
earned its keep in the Cocoa Touch frameworks with
features such as section handling and change monitoring, covered next.
Grouping results into sections
There are six qualifying zones in the World Cup: Africa, Asia, Oceania, Europe,
South America and North/Central America. The
Team
entity has a string attribute
named
qualifyingZone
that stores this information.
In this section, you'll split up the list of countries into their respective qualifying
zones.
NSFetchedResultsController
makes this very simple.
Let's see it in action. Go back to
viewDidLoad
and make the following change to the
fetched results controller's initializer:
fetchedResultsController
=
NSFetchedResultsController
(fetchRequest: fetchRequest,
managedObjectContext:
coreDataStack
.
context
,
sectionNameKeyPath:
"qualifyingZone"
,