Database Reference
In-Depth Information
3. With an
NSManagedObject
in hand, you set the
name
attribute using key-value
coding. You have to spell the KVC key (“name” in this case)
exactly
as it appears
on your data model, otherwise your app will crash at runtime.
4. You commit your changes to
person
and save to disk by calling
save
on the
managed object context. Note that
save
takes one parameter, which is a pointer
to an
NSError
; if there is ever an error with the save operation, you can inspect
the error and alert the user if necessary.
5. Congratulations! Your new managed object is now safely ensconced in your Core
Data persistent store. Insert the new managed object into the
people
array so
that it shows up in the table view when it reloads.
That's a little more complicated than an array of strings, but not too bad. Some of
the code here—getting the managed object context and entity—could be done just
once in your own
init
or
viewDidLoad
and then reused later. For simplicity, you're
doing it all at once in one method.
Build and run the app, and add a few names to the table view:
If the names are actually stored in Core Data, the HitList app should pass the
persistence test. Double-tap the Home button to bring up the fast app switcher.
Terminate the HitList app by flicking it upwards.
From Springboard, tap the HitList app to trigger a fresh launch. Wait, what
happened? The table view is empty: