ios 如何高效地迭代 NSSet (Objective-C) - 核心数据中的多对多关系表示?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12635256/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 20:28:12  来源:igfitidea点击:

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

iphoneobjective-cioscocoaoperating-system

提问by ikevin8me

To-Many relationships in Core Data are represented by NSSet (as automatically generated by using the Editor... Create NSManagedObject Subclass.. menu.

核心数据中的多对多关系由 NSSet 表示(使用编辑器自动生成...创建 NSManagedObject 子类...菜单。

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?

或其他方式?

采纳答案by Tommy

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; set the fetch request's returnsObjectsAsFaultsto NO and ensure any appropriate relationshipKeyPathsForPrefetchingare specified. 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.

话虽如此,如果你问是因为性能似乎是一个问题,那可能更多的是你花了很多时间在故障对象的核心数据上。一个聪明的举动是self in %@使用groups; 将获取请求设置returnsObjectsAsFaults为 NO 并确保relationshipKeyPathsForPrefetching指定了任何适当的请求。这样做的最终效果是,您将要迭代的所有数据都在一次到后备存储的行程中获取,而不是在大量单独的行程中按需获取其中的每一部分。