Database Reference
In-Depth Information
Measure the problem
Instead of Instruments, you'll use the
XCTest framework
to measure the
performance of the department list screen. XCTest is usually used for unit tests, but
there are some new performance testing tools available as of Xcode 6.
Note: For more information on unit tests and Core Data, check out Chapter 8,
“Unit Testing”
First, familiarize yourself with how the app creates the department list screen. Open
DepartmentListViewController.swift
and find the following code in
totalEmployeesPerDepartment
.
//1
let
fetchRequest =
NSFetchRequest
(entityName:
"Employee"
)
var
error:
NSError
? =
nil
var
fetchResults =
coreDataStack
.
context
.
executeFetchRequest
(fetchRequest,
error: &error)
if
fetchResults {
//2
var
uniqueDepartments = [
String
:
Int
]()
for
object
in
fetchResults {
let
employee = object
as
Employee
if
let
employeeDepartmentCount =
uniqueDepartments[employee.
department
] {
uniqueDepartments[employee.
department
] =
employeeDepartmentCount +
1
}
else
{
uniqueDepartments[employee.
department
] =
1
}
}
//3
var
results = [[
String
:
String
]]()
for
(department, headCount)
in
uniqueDepartments {
let
departmentDictionary:[
String
:
String
] =
[
"department"
:department,
"headCount"
:
String
(headCount)]