tags:

views:

29

answers:

3

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 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?

+1  A: 

Yes. Use -[NSFetchRequest setPropertiesToFetch:]. Only available in iOS 3 & OS X 10.6 and later, though.

Yuji
Many thanks! I am going to try this out now.
Mario
Seamus's advice is also good. If your dataset is really big, and if you're comfortable with bare sql, it might be easier for you to just use sql instead.
Yuji
Thanks Yuji, I just tried this out and it is exactly what I needed!
Mario
+1  A: 

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.

Seamus Campbell
+1  A: 

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.

TechZen