Read iOS Programming: The Big Nerd Ranch Guide, 3/e (Big Nerd Ranch Guides) Online
Authors: Aaron Hillegass,Joe Conway
Tags: #COM051370, #Big Nerd Ranch Guides, #iPhone / iPad Programming
In the model file, you described a new entity,
BNRAssetType
, that every item will have a to-one relationship to. You need a way for the user to set the
BNRAssetType
of
BNRItem
s and create new
BNRAssetType
s. Also, the
BNRItemStore
will need a way to fetch the
BNRAssetType
s. (Creating new
BNRAssetType
s is left as a challenge at the end of this chapter.)
In
BNRItemStore.h
, declare a new method.
In
BNRItemStore.m
, define this method. If this is the first time the application is being run – and therefore there are no
BNRAssetType
s in the store – create three default types.
Now you need change the user interface so that the user can see and change the
BNRAssetType
of the
BNRItem
in the
DetailViewController
.
Figure 16.12 Interface for BNRAssetType
Create a new
Objective-C class
template file and choose
NSObject
as the superclass. Name this class
AssetTypePicker
.
In
AssetTypePicker.h
, forward declare
BNRItem
, change the superclass to
UITableViewController
, and give it a
BNRItem
property.
This table view controller will show a list of the available
BNRAssetType
s. Tapping a button on the
DetailViewController
’s
view
will display it. Implement the data source methods and import the appropriate header files in
AssetTypePicker.m
. (You’ve seen all this stuff before.)
In
DetailViewController.xib
, add a new
UIButton
to the view. Create and connect the outlet and action as shown in
Figure 16.13
. The outlet
assetTypeButton
should be a weak instance variable. (Remember, you create and connect outlets by Control-dragging to
DetailViewController.h
.)
Figure 16.13 Add a UIButton
The following method and instance variable should now be declared in
DetailViewController.h
.
At the top of
DetailViewController.m
, import the header for this new table view controller.
Implement
showAssetTypePicker:
in
DetailViewController.m
.
And finally, update the title of the button to show the asset type of a
BNRItem
. In
DetailViewController.m
, add the following code to
viewWillAppear:
.
Build and run the application. Select a
BNRItem
and set its asset type.
In this chapter, you used SQLite via Core Data. If you’re curious about what SQL commands Core Data is executing, you can use a command-line argument to log all communications with the SQLite database to the console. From the
Product
menu, choose
Edit Scheme...
. Select the
Run Homepwner.app
item and the
Arguments
tab. Add two arguments:
-com.apple.CoreData.SQLDebug
and
1
.
Figure 16.14 Turning on Core Data logging
Build and run the application again. Make sure the debug area and console are visible so you can see the SQL logging. Add a few locations and inventory items; then navigate around the application looking at various items.
Relationships are fetched in a lazy manner. When you fetch a managed object with relationships, the objects at the other end of those relationship are
not
fetched. Instead, Core Data uses
faults
. There are to-many faults (which stand in for sets) and to-one faults (which stand in for managed objects). So, for example, when the instances of
BNRItem
are fetched into your application, the instances of
BNRAssetType
are not. Instead, fault objects are created that stand in for the
BNRAssetType
objects until they are really needed.
Figure 16.15 Object faults
An object fault knows what entity it is from and what its primary key is. So, for example, when you ask a fault that represents an asset type what its label is, you’ll see SQL executed that looks something like this:
(Why is everything prefixed with
Z_
? I don’t know. What is
OPT
? I don’t know, but I would guess it is short for
“
optimistic locking.
”
These details are not important.) The fault is replaced, in the exact same location in memory, with a managed object containing the real data.
Figure 16.16 After one fault is replaced
This lazy fetching makes Core Data not only easy to use, but also quite efficient.
What about to-many faults? Imagine that your application worked the other way: the user is presented with a list of
BNRAssetType
objects to select from. Then, the items for that asset type are fetched and displayed. How would this work? When the assets are first fetched, each one has a set fault that is standing in for the
NSSet
of item objects:
Figure 16.17 Set faults
When the set fault is sent a message that requires the
BNRItem
objects, it fetches them and replaces itself with an
NSSet
:
Figure 16.18 Set fault replaced
Core Data is a very powerful and flexible persistence framework, and this chapter has been just a quick introduction to its capabilities. For more details, we strongly suggest that you read Apple’s
Core Data Programming Guide
. Here are some of the things we have not delved into: