Database Reference
In-Depth Information
You saved to Core Data, but after a fresh app launch, the
people
array is empty!
The data is actually sitting there waiting, but you haven't fetched it yet.
Fetching from Core Data
To get data from your persistent store and into the managed object context, you
have to
fetch
it. Add the following method to
ViewController.swift
:
override
func
viewWillAppear(animated:
Bool
) {
super
.
viewWillAppear
(animated)
//1
let
appDelegate =
UIApplication
.
sharedApplication
().
delegate
as
AppDelegate
let
managedContext = appDelegate.
managedObjectContext
!
//2
let
fetchRequest =
NSFetchRequest
(entityName:
"Person"
)
//3
var
error:
NSError
?
let
fetchedResults =
managedContext.
executeFetchRequest
(fetchRequest,
error: &error)
as
[
NSManagedObject
]?
if
let
results = fetchedResults {
people
= results