Database Reference
In-Depth Information
NSTableView
, like
NSOutlineView
(as discussed in
NSOutlineView
, on page 148
), plays
very nicely with Core Data. This is especially true when the
NSTableView
is
backed by an
NSArrayController
that is feeding the data. It is possible to use
NSTableView
with a custom data source, if that is appropriate for the problem
at hand. However, when bound with an
NSArrayController
, the
NSTableView
can be
manipulated with other objects, such as the
NSSearchView
(discussed in a
moment), to produce interfaces that integrate smoothly and provide a great
user experience.
With this configuration, the
NSTableView
displays the data from the
NSArrayController
automatically and, thanks to KVO, stays in sync with the data stored in the
persistence layer.
NSArrayController
NSArrayController
is an extremely useful object when working with Core Data
because it is aware of the Core Data layer and knows how to talk to it without
any additional code on our part. When we configure it within Interface Builder,
all that we really need to give it is the
NSManagedObjectContext
and a data object
type. The rest of the work—retrieving the objects, updating those objects, and
creating new ones—is all handled for us.
NSArrayController
also understands relationships between objects when it is
working with Core Data. In our recipe application, we have one
NSArrayController
configured to manage
RecipeIngredient
objects. Based on our data model, these
are child objects that are bound to a specific recipe. Because
NSArrayController
understands these relationships, we can configure it to pull and display only
those
RecipeIngredient
objects that are connected to a
Recipe
object that is
selected in another
NSArrayController
. This again is made possible by KVC and
KVO. When we configure the
RecipeIngredient
's
NSArrayController
to provide only
those ingredients that are related to the specific recipe, what it is doing behind
the scenes is accessing the
Recipe
object and requesting its
ingredients
property
via KVC. In the
RecipeIngredient
' s
NSArrayController
, we bind the
NSManagedObjectContext
so that new ingredients can be added. In addition to properly being able to
create (and remove) objects from the persistence layer, the
NSArrayController
will
also manage the relationship between the newly created or removed
RecipeIn-
gredient
and the parent
Recipe
object.
All of this works because Core Data is the entire persistence layer and is
accessed in a consistent way no matter what object is being dealt with.
Because Core Data uses KVO and KVC, our controller objects do not need to
know very much about the objects, other than the name of the objects and
where they are stored. The rest is all resolved at runtime based on the settings