Database Reference
In-Depth Information
have quicker fetches. Don't worry—what's important is the change in time
you'll see after you modify the project.
Changes to improve performance
Open EmployeeListViewController.swift and find the following line of code in
employeeFetchRequest(department:) :
let fetchRequest = NSFetchRequest (entityName: "Employee" )
This code creates a fetch request using the Employee entity. You haven't set a batch
size, so it defaults to 0, which means no batching.
Now set the batch size on the fetch request to 10, like so:
let fetchRequest = NSFetchRequest (entityName: "Employee" )
fetchRequest. fetchBatchSize = 10
How do you come up with an optimal batch size? A good rule of thumb is to set the
batch size to about double the number of items that appear onscreen at any given
time. The employee list shows three to five employees onscreen at once, so 10 is a
reasonable batch size.
Verify the changes
Now that you've made the necessary change to the project, it's once again time to
see if you've actually improved the app.
To test this fix, first build and run the app and make sure it still works. Next launch
Instruments again (from Xcode, select Product and then Profile , or press I) and
repeat the steps you followed previously. Remember to scroll up and down the
employee list for about 20 seconds before clicking the Stop button in Instruments.
Note : To use the latest code, make sure you launch the app from Xcode,
which triggers a build, rather than just hitting the red button in Instruments.
This time, the Core Data Instrument should look like this:
Search WWH ::




Custom Search