循环遍历 CoreData ManagedObject 的关系

发布于 2024-12-18 13:46:18 字数 465 浏览 3 评论 0原文

我遇到过这样的情况:我有一个 ObjectA 列表。 ObjectAObjectB 具有多对多关系。我需要遍历 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月亮邮递员 2024-12-25 13:46:18

这不是您想要的答案,但也许它可以解决您的问题。为什么需要这些名字?

您不能使用谓词来循环嵌套列表吗?我不知道性能是否更好,但当您使用 CoreData 时,我会尝试。

我使用了一个谓词来循环嵌套列表,如下所示。我是用Xcode3的谓词编辑器做的。但Xcode4也有编辑器。

    NSPredicate *predicate =[NSPredicate predicateWithFormat:@"ANY keywords.word contains[cd] %@",  self.searchBar.text];

    [fetchedResultsControllerSearch.fetchRequest setPredicate:predicate];



NSError *error = nil;
if (![[self fetchedResultsControllerSearch] performFetch:&error]) {
    // Handle error
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);  // Fail
}      

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.

    NSPredicate *predicate =[NSPredicate predicateWithFormat:@"ANY keywords.word contains[cd] %@",  self.searchBar.text];

    [fetchedResultsControllerSearch.fetchRequest setPredicate:predicate];



NSError *error = nil;
if (![[self fetchedResultsControllerSearch] performFetch:&error]) {
    // Handle error
    NSLog(@"Unresolved error %@, %@", error, [error userInfo]);
    exit(-1);  // Fail
}      
眼眸 2024-12-25 13:46:18

我对你的循环有点困惑。 names 中最终出现的唯一结果是 中最后一个 a.objectBsb.name。列表A。

尝试如下操作怎么样:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"objectB"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.objectAs contains %@", listA); 
fetchRequest.predicate = predicate;

// execute

这将返回在其 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 the b.name's from the very last a.objectBs in listA.

How about trying something like:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"objectB"];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"self.objectAs contains %@", listA); 
fetchRequest.predicate = predicate;

// execute

This would return all of the objectBs that have an objectA in their objectA relation. You could then call objectB.name at the point you require the name.

PS the naming of your example makes this 50 million times harder to follow.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文