sco*_*ter 9 database indexing core-data objective-c ios
我正在开发一款iOS闪存卡式学习应用程序,在加载时需要从Core Data中获取大量数据.但是我需要的数据是实体的一个相当特定的子集,基于用户设置,所以有多个谓词涉及测试等价.我发现这些提取速度非常慢,根据对SQLite的研究,我认为索引在这里是一个不错的选择.
现在,我理解(主要是通过阅读其他stackoverflow问题)SQLite和Core Data是两个不同的,基本上是正交的东西,不应该混淆.但是我的理解是你应该通过Core Data来完成任何类型的数据库工作和调整; 在优化或设计应用程序中的对象持久性时,不应试图绕过并直接使用SQLite.
但我唯一可以在Core Data中找到索引的是模型中每个属性的一个"索引"复选框.而这只是没有做我正在寻找的那种优化.
这是获取请求,目前:
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"SKUserItem" inManagedObjectContext:context];
fetchRequest.entity = entity;
NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"next" ascending:YES] autorelease];
fetchRequest.sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
NSMutableArray *predicates = [NSMutableArray arrayWithCapacity:6];
[predicates addObject:[NSPredicate predicateWithFormat:@"next < %f", now() + (60.0*60.0*24.0)]];
[predicates addObject:[NSPredicate predicateWithFormat:@"next > %f", nextOffset]];
[predicates addObject:[NSPredicate predicateWithFormat:@"user == %@", user]];
[predicates addObject:[NSPredicate predicateWithFormat:@"langRaw == %d", lang]];
NSArray *stylePredicates = [NSArray arrayWithObjects:[NSPredicate predicateWithFormat:@"styleRaw == %d", SK_SIMP_AND_TRAD], [NSPredicate predicateWithFormat:@"styleRaw == %d", self.style], nil];
[predicates addObject:[NSCompoundPredicate orPredicateWithSubpredicates:stylePredicates]];
if([self.parts count] == 4 || (self.lang == SK_JA && [self.parts count] == 3))
; // don't have to filter by parts; they're studying all of them
else {
NSMutableArray *partPredicates = [NSMutableArray arrayWithCapacity:[self.parts count]];
for(NSString *part in self.parts)
[partPredicates addObject:[NSPredicate predicateWithFormat:@"partRaw == %d", partCode(part)]];
[predicates addObject:[NSCompoundPredicate orPredicateWithSubpredicates:partPredicates]];
}
NSPredicate *compoundPredicate = [NSCompoundPredicate andPredicateWithSubpredicates:predicates];
fetchRequest.predicate = compoundPredicate;
Run Code Online (Sandbox Code Playgroud)
所以这个fetch所做的基本上是下一个(给定项目到期的时间)和用户名,正在研究的语言,正在研究的样式(中文有简化和传统)和正在研究的部分(写作,音调)的过滤器,阅读或定义),仅在"下一个"范围内获取.以下是我从调整和摆弄这些事情中学到的一些简短列表:
我来自Google App Engine,所以我习惯了他们在那里提供的索引.基本上我想要那种索引,但是通过Core Data应用于SQLite.我找到了有关在SQLite中添加索引的信息,我想要的那种,但是通过Core Data进行这种索引,我找不到任何关于它的信息.
Dan*_*ert 18
你想要的是Core Data在iOS 5.0及更高版本中支持的复合索引.
您可以在Xcode中进行设置:实体检查器具有索引部分,或者如果您要创建输入NSEntityDescription
代码,请使用-setCompoundIndexes:
.
如果你使用Xcode,你可以在索引部分添加一行说明
next,user,langRaw
Run Code Online (Sandbox Code Playgroud)
这样SQL就可以为您的查询使用索引.
归档时间: |
|
查看次数: |
2669 次 |
最近记录: |