NSFetchRequest setPropertiesToGroupBy:返回所有匹配的实体? [英] Does NSFetchRequest setPropertiesToGroupBy: return all entities that match?
问题描述
我正在为iOS开发一个应用程序,我找不到有关新的 NSFetchRequest setPropertiesToGroupBy:
方法的文档。
我有一堆与多个实体B对象有关系的实体A对象。
我想获取所有与特定对象匹配的实体B对象模式,并由实体A将它们分组。这是为了我没有为每个实体A单独的DB提取。结果将理想地被结构化,使得我可以迭代相等的实体As的组,然后将具有所有实体B匹配来自一个获取请求的模式。
我可以看到 setPropertiesToGroupBy:
方法应该让我通过逆B-> A关系,并返回 NSDictionaries
的数组。但是这是否意味着 NSArray
中的每个项目对应于实体A的一个组?并且相应的 NSManagedObject
子类可以在 NSDictionary
?
< class =h2_lin>解决方案
没有。有两种方法可以执行此操作:
- 使用多个请求
- A关系为A→B关系的实体。然后包括
NSSortDescriptor
,以按B排序,并将结果分成组。
后者可能更快,因为内存中分割可避免重复的DB访问和提取请求解析。
I'm developing an app for iOS, and I can't find much documentation on the new NSFetchRequest setPropertiesToGroupBy:
method.
I have a bunch of entity A objects that have relationships with multiple entity B objects.
I want to fetch all entity B objects that match a specific pattern and group them by entity A. This is so that I don't have a separate DB fetch for every entity A. The results would ideally be structured so that I could iterate over groups of equal entity As, and then would have all the entity Bs matching the pattern from one fetch request.
I can see that the setPropertiesToGroupBy:
method should let me group by the inverse B->A relationship, and returns an array of NSDictionaries
. But does that mean each item in the NSArray
corresponds to one group by entity A? And that the corresponding NSManagedObject
subclasses can be found somewhere in the NSDictionary
?
No it doesn't. There are two ways to do this:
- Use multiple requests
- Have one request to get all A entities where the inverse A->B relationship is in a group of Bs. Then include an
NSSortDescriptor
to sort by B and separate the results in to groups.
The latter is probably quicker because an in-memory split avoids repeated DB accesses and fetch request parsing.
这篇关于NSFetchRequest setPropertiesToGroupBy:返回所有匹配的实体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!