为什么这段代码这么慢? (核心数据和 NSSet)

发布于 2024-10-08 07:53:45 字数 1062 浏览 6 评论 0原文

我有一个应用程序,我正在实施全文搜索。我有 2 个实体:关键字和文章,它们之间具有多对多关系。 有问题的代码是这样的:

            keywordsInRange = [[[CoreDataManager sharedManager] managedObjectContext]  executeFetchRequest:request error:&err];



            for(Keywords* word in keywordsInRange) {
                NSDate *methodStart = [NSDate date];

                [mySet addObjectsFromArray:[word.article allObjects]];

                NSDate *methodFinish = [NSDate date];
                NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
                NSLog(@"Keyword Search Exec Time: %.3f", executionTime);
            }

输出是这样的:

Keyword Search Exec Time: 0.235  //added 1 article
Keyword Search Exec Time: 0.216  //added 6 articles
Keyword Search Exec Time: 0.211  //etc
Keyword Search Exec Time: 0.205
Keyword Search Exec Time: 0.204

如您所见,当我在设备(iPad iOS 4.2.1)上进行测试时,添加链接到集合中的关键字的所有文章需要花费出乎意料的长时间。当我在模拟器中测试时,时间是:

0.029
0.026
0.026
0.026
0.026

错误在哪里,我能做些什么来加快速度?

I have an app and I'm implementing full text search. I have 2 enitites: Keywords and Articles with a many-to-many relationship between them.
The problematic piece of code is this:

            keywordsInRange = [[[CoreDataManager sharedManager] managedObjectContext]  executeFetchRequest:request error:&err];



            for(Keywords* word in keywordsInRange) {
                NSDate *methodStart = [NSDate date];

                [mySet addObjectsFromArray:[word.article allObjects]];

                NSDate *methodFinish = [NSDate date];
                NSTimeInterval executionTime = [methodFinish timeIntervalSinceDate:methodStart];
                NSLog(@"Keyword Search Exec Time: %.3f", executionTime);
            }

the output is this:

Keyword Search Exec Time: 0.235  //added 1 article
Keyword Search Exec Time: 0.216  //added 6 articles
Keyword Search Exec Time: 0.211  //etc
Keyword Search Exec Time: 0.205
Keyword Search Exec Time: 0.204

as you can see, to add all articles linked to a keyword in the set takes unexpectedly long time when I test on the device (iPad iOS 4.2.1). When I test in the simulator the times are:

0.029
0.026
0.026
0.026
0.026

Where is the mistake, what can I do to speed things up?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

他是夢罘是命 2024-10-15 07:53:45

我不是 CoreData 的专家,但它不是延迟加载文章吗?如果是,则意味着针对每个关键字对存储执行查询。

I'm not an expert of CoreData but isn't it lazily loading articles ? If yes, it means there a query executing against the store for every keyword.

戈亓 2024-10-15 07:53:45

尝试在上面代码的第一行之前调用 [request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"article"]];

否则,循环中的每次迭代都会生成一次新的文章提取,尽管您实际上只需要一篇文章。

在这种情况下,使用 SQLDebug 查看实际生成的 SQL 语句通常会很有帮助。
为此,右键单击可执行文件,单击“获取信息”,转到“参数”选项卡,然后在“启动时传递的参数”中添加 -com.apple.CoreData.SQLDebug 1

Try calling [request setRelationshipKeyPathsForPrefetching:[NSArray arrayWithObject:@"article"]]; before the first line of your code above.

Otherwise, the each iteration in the loop generates a new fetch for the articles although you really only need one.

It is often helpful in this case to use SQLDebug to see the actual SQL statements being generated.
To do so, right-click on your executable, click on Get Info, go to the Arguments tab and add -com.apple.CoreData.SQLDebug 1 in "Arguments to be passed on launch".

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