gpt4 book ai didi

objective-c - 优化CoreData全文查询

转载 作者:行者123 更新时间:2023-12-03 17:25:23 24 4
gpt4 key购买 nike

我有大量代表文件的 CoreData 对象。我需要定期搜索该集合并查找给定路径中存在的文件。目前,我正在构建以下 NSPredicate 并执行相当基本的executeFetchRequest 来查找与我的查询匹配的所有结果。

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"path ==[c] %@", receivedPath];

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Files" inManagedObjectContext:self.moc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entity];
[request setPredicate:predicate];
[request setFetchBatchSize:10];

NSError *error = nil;
NSArray *results = [self.moc executeFetchRequest:request error:&error];
[request release];

我遇到的问题是这个函数被调用得相当频繁,并且executeFetchRequest 占据了执行时间的很大一部分(Instruments 告诉我该函数总共占了大约 49.2%,其中 98.1% 花费在在executeFetchRequest 调用上)。我使用 NSSQLiteStoreType 作为我的持久存储,并在我的 MOM 中索引了 path 属性。

我的问题是,我该如何优化它?我考虑过设置一个小写路径属性,并在比较中放弃 [c] 修饰符,但不确定这会对执行时间产生什么影响(如果有)。

任何帮助将不胜感激。

最佳答案

删除[c]应该会有所不同。如果我没记错的话, ==[c] 可能被翻译为 SQLite LIKE 运算符,而 SQLite 无法使用索引。说到这里,您的模型中是否已将属性标记为索引?如果没有,您应该这样做,但同样,对于不区分大小写的搜索,这可能没有什么区别。

关于objective-c - 优化CoreData全文查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6753792/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com