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

核能气质少年 提交于 2019-12-03 07:50:41

问题


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:


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]];


来源:https://stackoverflow.com/questions/1448136/core-data-to-many-relationships-are-they-lazy-load

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!