Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Core Data to-many relationships. Are they Lazy Load?

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

like image 897
gonso Avatar asked Sep 19 '09 08:09

gonso


1 Answers

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]];
like image 51
Massimo Cafaro Avatar answered Oct 04 '22 21:10

Massimo Cafaro