【发布时间】:2012-02-29 10:20:35
【问题描述】:
所以,我试图从核心数据中获取大约 2000 个对象,并试图找出获取它们的最快方法。
没有 NSPredicates:
当我添加这个块(下图)时,if 语句占用了执行总时间的 90%,这是可以理解的,所以我将其注释掉。
Block {
NSError *error;
NSManagedObjectContext *context = <#Get the context#>;
// The IF block
/* if (![context save:&error])
{
NSLog(@"error %@", error);
}
*/
// The block above takes 90% of the total time of fetch
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *theEntity = [NSEntityDescription
entityForName:@"EntityName" inManagedObjectContext:context];
[fetchRequest setEntity:theEntity];
NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:&error];
}
使用 NSPredicates:
我将谓词设置为
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"handle == %@",handle];
并执行与Block()中所示相同的提取
可以看出,总体时间正好相反——如果我使用IF BLOCK ~ 6 秒,它会快 90% 左右。
否则,如果该块被评论,获取时间是很多〜 3 分钟。仅当为fetchRequest 设置predicate 时才会发生这种情况。
谁能解释一下?
也就是说,
/*
Save + straight fetch = slow, due to saving.
Straight fetch - pretty fast.
Predicate fetch, slow.
Save + predicate fetch, much faster?
*/
【问题讨论】:
-
显示更多代码。什么是“IF块”?你如何使用那个块?以及如何创建 NSFetchRequest?
-
@MatthiasBauch 请检查编辑。 !
-
所以让我直截了当,因为我发现这个问题写得有点令人困惑。保存 + 直接提取 = 慢,由于保存。直接获取 - 相当快。谓词获取,慢。保存 + 谓词提取,快得多?
-
@jrturton 是的!没错!
标签: iphone objective-c xcode cocoa-touch core-data