Database Reference
In-Depth Information
//4
if let bowties = results {
populate (bowties[ 0 ])
} else {
println ( "Could not fetch \(error) , \(error!. userInfo ) " )
}
}
This is where you fetch the Bowtie s from Core Data and populate the UI. Step by
step, here's what you're doing with this code:
1. You call the insertSampleData method you implemented earlier. Since
viewDidLoad can be called every time the app is launched, insertSampleData itself
performs a fetch to make sure it isn't inserting the sample data into Core Data
multiple times.
2. You create a fetch request for the purpose of fetching the newly inserted Bowtie
entities. The segmented control has tabs to filter by color, so the predicate adds
the condition to find the bow ties that match the selected color. Predicates are
both very flexible and very powerful—you'll read more about them in chapter 4.
For now, you should know that this particular predicate is looking for bow ties
that have their searchKey property set to the segmented control's first button
title, “R” in this case.
3. As always, the managed context does the heavy lifting for you. It executes the
fetch request you crafted moments earlier and returns an array of Bowtie objects.
4. You populate the user interface with the first bow tie in the results array. If there
was an error, print the error to the console.
You haven't defined the populate method yet, so Xcode throws a warning.
Implement it as follows:
func populate(bowtie: Bowtie ) {
imageView . image = UIImage (data:bowtie. photoData )
nameLabel . text = bowtie. name
ratingLabel . text = "Rating: \(bowtie. rating . doubleValue ) /5"
timesWornLabel . text =
"# times worn: \(bowtie. timesWorn . integerValue ) "
let dateFormatter = NSDateFormatter ()
dateFormatter. dateStyle = . ShortStyle
dateFormatter. timeStyle = . NoStyle
lastWornLabel . text = "Last worn: " +
dateFormatter. stringFromDate (bowtie. lastWorn )
Search WWH ::




Custom Search