简体   繁体   English

如何有效地迭代NSSet(Objective-C) - 核心数据中的To-Many关系表示?

[英]How to iterate an NSSet (Objective-C) - To-Many relationship representation in Core Data - efficiently?

To-Many relationships in Core Data are represented by NSSet (as automatically generated by using the Editor... Create NSManagedObject Subclass.. menu. Core Data中的To-Many关系由NSSet表示(通过使用Editor ... Create NSManagedObject Subclass ..菜单自动生成)。

Which is the most efficient way to iterate an NSSet* ? 哪种迭代NSSet *最有效?

NSSet* groups = [contact groups];
for(Group* group in groups) {
    NSString* groupName = [group name];
}

or 要么

NSSet* groups2 = [contact groups];
NSArray* groupsArray = [groups2 allObjects];
for(Group* group in groupsArray) {
    NSString* groupName = [group name];
}

or another way? 或另一种方式?

The first is probably more efficient. 第一个可能更有效率。 If the latter were more efficient then Apple would simply use that route to implement the former. 如果后者效率更高,那么Apple只会使用该路线来实现前者。

That being said, if you're asking because performance seems to be an issue it's probably more that you're spending a lot of time on the Core Data stuff of faulting objects. 话虽这么说,如果你问因为性能似乎是一个问题,你可能会花费大量时间在故障对象的核心数据上。 A smart move would be to do a fetch on self in %@ with groups ; 一个聪明的举动就是self in %@使用groups进行self in %@获取; set the fetch request's returnsObjectsAsFaults to NO and ensure any appropriate relationshipKeyPathsForPrefetching are specified. 将获取请求的returnsObjectsAsFaults为NO,并确保指定任何适当的relationshipKeyPathsForPrefetching The net effect of that will be that all the data you're about to iterate through is fetched in a single trip to the backing store rather than each individual piece of it being fetched on demand in a large number of separate trips. 这样做的最终结果是,您要迭代的所有数据都会在一次访问后备存储中获取,而不是在大量单独的行程中按需获取每个数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM