Does Core Data allow one to generate a statement like select FirstName, LastName from Employee instead of the entire row?
In the example of Departments/Employees, let's say I want to write a navagation controller style application to display the Departments available, and when clicked, the Employees available in that Department. Lets suppose that the Employee object is huge for whatever reason. I don't see why I would need to retrieve a huge set开发者_StackOverflow中文版 of objects in the EmployeesViewController just to display their names in a list view. Is there anyway I can just request the Name field (perhaps two: FirstName, LastName) for all Employees in a given Department?
Just as a side note:
Beware of the dangers of premature optimization. You shouldn't bother trying to tweak a fetch unless you've tested and found that the bare bones fetch is actually a problem. Core Data has a large number of optimizations under the hood that make it far more efficient than it would appear at first glance. In most cases, its an utter waste of time to tweak a fetch.
If you have a table that displays thousands of objects, you will usually only have a few dozen live objects in memory at anyone time. By default, Core Data fetches objects as faults i.e. ghost of the objects without the values of their attributes loaded. Only when you access a specific attribute of a specific object will that object load completely into memory.
If you come from an SQL background, you may be used to having to manually juggle objects created from SQL. Core Data handles all that for you and does so much more efficiently than you can do manually. You're intuitive assumption you developed working with SQL about the degree of manual optimization you need to do will be way off when applied to Core Data.
Instead, pick the simplest and easiest method first and optimize only if you test and find a bottleneck. Most of the time, you will find that your fretting over optimization was completely unwarranted.
If your employee object is very large, and loading it is too expensive, consider partitioning it with one-to-one relationships. If you have a list of fields like:
Employee:
id
name
SSN
DOB
home_address
home_phone
department
office_number
office_phone
manager
job_title
salary_class
start_date
...
You could break this up into:
Employee
id
department
manager
...
EmployeePersonalInfo
employee_id
SSN
DOB
home_address
home_phone
EmployeeJobDescription
employee_id
job_title
start_date
salary_class
And so on. For your most common objects, limiting the fetched data to the most pertinent and commonly-accessed field is good practice.
Yes. Use -[NSFetchRequest setPropertiesToFetch:]
. Only available in iOS 3 & OS X 10.6 and later, though.
精彩评论