Database Reference
In-Depth Information
results.
append
(departmentDictionary)
}
return
results
}
else
{
println
(
"ERROR:
\(error?.
localizedDescription
)
"
)
return
[[
String
:
String
]]()
}
This code does the following:
1. It creates a fetch request with the
Employee
entity and then fetches all
employees.
2. It iterates though the employees and builds a dictionary, where the key is the
department name and the value is the number of employees in that department.
3. It builds an array of dictionaries with the required information for the department
list screen.
How could you measure the performance of this code?
Open
DepartmentListViewControllerTests.swift
(notice the
Tests
suffix in the
filename) and add the following code after the
tearDown
function:
func
testTotalEmployeesPerDepartment() {
measureMetrics
([
XCTPerformanceMetric_WallClockTime
],
automaticallyStartMeasuring:
false
) {
let
departmentList =
DepartmentListViewController
()
departmentList.
coreDataStack
=
CoreDataStack
()
self
.
startMeasuring
()
let
items = departmentList.
totalEmployeesPerDepartment
()
self
.
stopMeasuring
()
}
}
This function uses
measureMetrics
to see how long code takes to execute.
You have to set up a new Core Data stack each time so that you aren't just taking
advantage of Core Data's excellent caching to make the subsequent test runs really
fast!
Inside the block, you first create a
DepartmentListViewController
and give it a
CoreDataStack
. Then, you call
totalEmployeesPerDepartment
to retrieve the number
of employees per department.
Now you need to run this test. From Xcode's menu bar, select
Product
and then
Test
, or press
⌘
U. This will build the app and run the tests.