Database Reference
In-Depth Information
Changes to improve performance
In the previous example, you used NSExpression to group the data and provide a
count of employees by department instead of returning the actual records
themselves. This time you need to do the same thing, retrieve a count without
getting all the actual records.
Open EmployeeDetailViewController.swift and add the following code to the
class.
public func salesCountForEmployeeFast(employee: Employee ) -> String {
let fetchRequest = NSFetchRequest (entityName: "Sale" )
let predicate =
NSPredicate (format: "employee == %@" , employee)
fetchRequest. predicate = predicate
var error : NSError ?
let context = employee. managedObjectContext
let results =
context. countForFetchRequest (fetchRequest, error: &error)
return " \(results) "
}
This code is very similar to the function you reviewed in the last section. The
primary difference is that instead of calling executeFetchRequest , you are now
calling countForFetchRequest .
Find the following line of code in configureView() :
label. text = salesCountForEmployee (employee)
This line of code uses the old sales count function to populate the label on the
department details screen. Replace it by calling the function you just created:
label. text = salesCountForEmployeeFast (employee)
Verify the changes
Now that you've made the necessary changes to the project, it's once again time to
see if you've improved the app.
Open EmployeeDetailViewControllerTests.swift and add a new function to test
the totalEmployeesFast function you just created.
func testCountSalesFast() {
self . measureMetrics ([ XCTPerformanceMetric_WallClockTime ],
automaticallyStartMeasuring: false , forBlock: {
let employee = self . getEmployee ()
let employeeDetails = EmployeeDetailViewController ()
Search WWH ::




Custom Search