使用SQLite后端修复预测的NSFetchedResultsController / NSFetchRequest性能? [英] Fixing predicated NSFetchedResultsController/NSFetchRequest performance with SQLite backend?

查看:192
本文介绍了使用SQLite后端修复预测的NSFetchedResultsController / NSFetchRequest性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一系列的NSFetchedResultsController控制一些表视图,它们在设备上的性能是糟糕的,大约为秒。因为它都运行在主线程,它阻止了我的应用程序在启动时,这是不是很大。

I have a series of NSFetchedResultsControllers powering some table views, and their performance on device was abysmal, on the order of seconds. Since it all runs on main thread, it's blocking my app at startup, which is not great.

我调查,结果的谓词是问题:

I investigated and turns out the predicate is the problem:

NSPredicate *somePredicate = [NSPredicate predicateWithFormat:@"ANY somethings == %@", something];
[fetchRequest setPredicate:somePredicate];

即fetch实体,称为things,与实体具有多对多关系什么。

I.e the fetch entity, call it "things", has a many-to-many relation with entity "something". This predicate is a filter that limits the results to only things that have a relation with a particular "something".

当我删除谓词以进行测试时,获取时间(如果有的话) initial performFetch:call)从4秒减少到100ms或更少,这是可以接受的。我感到困扰的是,因为它否定了很多我希望获得的核心数据和NSFRC,否则看起来像一个强大的工具。

When I removed the predicate for testing, fetch time (the initial performFetch: call) dropped (for some extreme cases) from 4 seconds to around 100ms or less, which is acceptable. I am troubled by this, though, as it negates a lot of the benefit I was hoping to gain with Core Data and NSFRC, which otherwise seems like a powerful tool.

所以,我的问题是,我如何优化这种性能?我使用谓词错了吗?我应该以某种方式修改模型/模式吗?还有什么其他方法来解决这个问题?这种性能是否会降低?

So, my question is, how can I optimize this performance? Am I using the predicate wrong? Should I modify the model/schema somehow? And what other ways there are to fix this? Is this kind of degraded performance to be expected? (There are on the order of hundreds of <1KB objects.)

编辑详细信息

这里是代码:

[fetchRequest setFetchLimit:200];
NSLog(@"before fetch");
BOOL success = [frc performFetch:&error];
if (!success) {
    NSLog(@"Fetch request error: %@", error);
}
NSLog(@"after fetch");

更新日志(以前,我有一些应用程序无效率降低性能,这些是更新的日志应该是接近最佳,你可以得到在我目前的环境):

Updated logs (previously, I had some application inefficiencies degrading the performance here. These are the updated logs that should be as close to optimal as you can get under my current environment):

2010-02-05 12:45:22.138 Special Ppl[429:207] before fetch
2010-02-05 12:45:22.144 Special Ppl[429:207] CoreData: sql: SELECT DISTINCT 0, t0.Z_PK, t0.Z_OPT, <model fields> FROM ZTHING t0 LEFT OUTER JOIN Z_1THINGS t1 ON t0.Z_PK = t1.Z_2THINGS WHERE  t1.Z_1SOMETHINGS = ? ORDER BY t0.ZID DESC LIMIT 200
2010-02-05 12:45:22.663 Special Ppl[429:207] CoreData: annotation: sql connection fetch time: 0.5094s
2010-02-05 12:45:22.668 Special Ppl[429:207] CoreData: annotation: total fetch execution time: 0.5240s for 198 rows.
2010-02-05 12:45:22.706 Special Ppl[429:207] after fetch

如果我做没有谓词的同一个提取(通过注释掉问题开头的两行):

If I do the same fetch without predicate (by commenting out the two lines in the beginning of the question):

2010-02-05 12:44:10.398 Special Ppl[414:207] before fetch
2010-02-05 12:44:10.405 Special Ppl[414:207] CoreData: sql: SELECT 0, t0.Z_PK, t0.Z_OPT, <model fields> FROM ZTHING t0 ORDER BY t0.ZID DESC LIMIT 200
2010-02-05 12:44:10.426 Special Ppl[414:207] CoreData: annotation: sql connection fetch time: 0.0125s
2010-02-05 12:44:10.431 Special Ppl[414:207] CoreData: annotation: total fetch execution time: 0.0262s for 200 rows.
2010-02-05 12:44:10.457 Special Ppl[414:207] after fetch

20倍的差异。 500ms不是那么好,而且似乎没有办法在后台线程或其他优化,我可以想到。 (除了进入一个二进制存储,这是一个非问题,所以我可以这样做。二进制存储性能一直〜100ms以上200对象谓词查询。)

20-fold difference in times. 500ms is not that great, and there does not seem to be a way to do it in background thread or otherwise optimize that I can think of. (Apart from going to a binary store where this becomes a non-issue, so I might do that. Binary store performance is consistently ~100ms for the above 200-object predicated query.)

(我之前嵌套了另一个问题,我现在

(I nested another question here previously, which I now moved away).

推荐答案

对我来说,实际的解决方案是:只是切换到具有更好的性能的Core Data二进制存储。我没有调查是否可以改善我的当前应用程序情况下的Core Data SQLite性能。

The practical solution to this for me was to just switch to Core Data binary store that has much better performance. I did not investigate whether I can improve Core Data SQLite performance with my current app situation.

这篇关于使用SQLite后端修复预测的NSFetchedResultsController / NSFetchRequest性能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆