【发布时间】:2013-02-02 19:10:04
【问题描述】:
我遇到了一个问题,我确实解决了它,但我觉得它效率很低,这涉及遍历父/子引用的核心数据(iOS)实体层次结构,以计算附加到某些项目的数量实体。
让我更具体一点。我有两种类型的实体:Category 和 Attachment。
Category 实体通过父/子引用链接。附件以多对一的形式链接到Categories(一个类别的多个附件)。
如果我想计算属于给定Category 层次结构的附件数量,有没有比这更有效的NSFetchRequest?
NSInteger count = 0;
NSMutableArray *stack = [[NSMutableArray alloc] init];
[stack addObject:targetCategory];
while([stack count] > 0)
{
Category *current = [stack lastObject];
[stack removeLastObject];
count += current.attachments.count;
for (Category *cat in current.children)
{
[stack addObject:cat];
}
}
targetCategory 是选择的根类别。
提前致谢。
【问题讨论】:
标签: ios core-data count hierarchy