views:

301

answers:

1

Hi

I have the typical model in Core Data (for iPhone) with Departments and Employesss (Department ->> Employee).

I dont want to load all employees of a department each time I load it, so I thought I'd create the Employee as a Fetched Property. I thought I could define some Predicate like this:

employee.deparmentId = department.departmentId

but I couldn't (with Xcode graphic editor).

Now, reading some posts, Im inclined to define a relationship to-many for Department to Employee (and its reverse) and use it.

My question is: Are to-many relationship Lazy Loading? or is it going to load all my Employees in the "database" each time I create (retrieve) a list of departments?

Thanks in advance Gonso

+1  A: 

No, objects related to to-many and to-one relationships are loaded lazily by default. However, If you need to access many of them each time you fetch a Department, then for performance reasons you may ask Core Data to load them simultaneously (this is called pre-fetching). You can do this as follow:

[fetchRequest setReturnsObjectsAsFaults:NO];
[fetchRequest setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObjects:@"employees", nil]];
unforgiven
on the contrary, I DONT want to load Employees unless Im going to work with them. More over, most of the times I wont list all empoyees (i.e. run through the Set) but get a sub set (i.e. run a fetch).Thanks.
gonso