循环遍历 CoreData ManagedObject 的关系
我遇到过这样的情况:我有一个 ObjectA
列表。 ObjectA
与ObjectB
具有多对多关系。我需要遍历 ObjectA
列表并存储 ObjectB
引用的所有名称。
我目前执行此操作的方式表明,使用 Instruments 时实际的 for 循环占执行时间的 89.5%:
for (ObjectA *a in listA) {
[names removeAllObjects];
for (ObjectB *b in a.objectBs) { //This is 89.5% of the execution time
[names addObject:b.name];
}
}
有哪些方法可以更好地处理这个问题?
I have a situation where I have a list of ObjectA
. ObjectA
has a many-to-many relationship with ObjectB
. I need to iterate through the list of ObjectA
and store all the names of the ObjectB
references.
The way I am currently doing this points to the actual for-loop as being 89.5% of execution time using Instruments:
for (ObjectA *a in listA) {
[names removeAllObjects];
for (ObjectB *b in a.objectBs) { //This is 89.5% of the execution time
[names addObject:b.name];
}
}
What are some ways to handle this better?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是您想要的答案,但也许它可以解决您的问题。为什么需要这些名字?
您不能使用谓词来循环嵌套列表吗?我不知道性能是否更好,但当您使用 CoreData 时,我会尝试。
我使用了一个谓词来循环嵌套列表,如下所示。我是用Xcode3的谓词编辑器做的。但Xcode4也有编辑器。
This is not the answer you are asking for but maybe it solves your problem. Why do you need those names?
Couldn't you use a predicate for looping through nested lists? I don't know if the performance is better but as you are using CoreData I would try.
I used a predicate that loops through nested lists like this. I made it with the predicate editor of Xcode3. But Xcode4 has also a editor.
我对你的循环有点困惑。
names
中最终出现的唯一结果是中最后一个
a.objectBs
的b.name
。列表A。尝试如下操作怎么样:
这将返回在其
objectA
关系中具有objectA
的所有objectB
。然后,您可以在需要名称时调用objectB.name
。PS,你的例子的命名使这个更难理解 5000 万倍。
I'm a bit confused by your loop. The only results that would end up in
names
would be the theb.name
's from the very lasta.objectBs
inlistA
.How about trying something like:
This would return all of the
objectBs
that have anobjectA
in theirobjectA
relation. You could then callobjectB.name
at the point you require the name.PS the naming of your example makes this 50 million times harder to follow.