Database Reference
In-Depth Information
}()
You'll use this lazily instantiated
NSPredicate
to calculate the number of venues that
fall into the lowest price category.
Note:
NSPredicate
supports key paths. This is why you can drill down from the
Venue
entity into the
PriceInfo
entity using
priceInfo.priceCategory
.
Next, implement the following method in
FilterViewController
:
func
populateCheapVenueCountLabel()
{
// $ fetch request
let
fetchRequest =
NSFetchRequest
(entityName:
"Venue"
)
fetchRequest.
resultType
= .CountResultType
fetchRequest.
predicate
=
cheapVenuePredicate
var
error:
NSError
?
let
result =
coreDataStack
.
context
.
executeFetchRequest
(fetchRequest,
error: &error)
as
[
NSNumber
]?
if
let
countArray = result {
let
count = countArray[
0
].
integerValue
firstPriceCategoryLabel
.
text
=
"
\(count)
bubble tea places"
}
else
{
println
(
"Could not fetch
\(error)
,
\(error!.
userInfo
)
"
)
}
}
This method creates a fetch request to fetch
Venue
entities. You then set the result
type to .
CountResultType
and set the fetch request's predicate to the lazy variable
you defined moments ago.
When you set a fetch result's result type to
NSCountResultType
, the return value
becomes an optional Swift array containing a single
NSNumber
. The integer inside the
NSNumber
is the total count you're looking for.