objective-c – 使用Core Data可以使用复杂的索引吗?
我正在研究一种iOS闪存卡风格的学习应用程序,在加载时,需要从Core Data获取一堆数据.但是,我需要的数据是实体的一个相当特定的子集,基于用户设置,所以有多个谓词涉及测试等价.我发现这些提取速度非常慢,并且基于SQLite的研究,我认为一个索引将是一个很好的选择.
现在,我明白(主要是从阅读其他stackoverflow问题),SQLite和核心数据是两个不同的基本正交的东西,不应该被混淆.但是我也理解,你应该通过核心数据来做任何数据库的工作和调整;在优化或设计应用程序中的对象永久性时,不应尝试绕过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; 所以本质上这个提取是什么排序在下一个(给定的项目到期的时间)和筛选用户名,正在研究的语言,正在研究的风格(中文有简化和传统)和被研究的部分(写作,音调,阅读或定义),并且仅在“下一个”范围内提取.以下是我从调整中学到的简短列表: >它总是扫描整个表,或者似乎.虽然下一个被索引,即使我迫使它搜索一个范围,我知道将不会返回任何东西,仍然需要几秒钟的抓取完成. 我来自Google App Engine,所以我习惯了他们在那里提供的索引.基本上我想要这种索引,但是通过核心数据应用于SQLite.我发现关于在SQLite中添加索引的信息,我想要的那种,但是通过Core Data进行这种索引,我找不到任何信息. 解决方法
您想要的是核心数据在iOS 5.0及更高版本中支持的复合索引.
您可以在Xcode中进行设置:实体检查器具有索引部分,或者如果要在代码中创建NSEntityDescription,请使用-setCompoundIndexes :. 如果您使用Xcode,您可以在“索引”部分中添加一行 next,user,langRaw 这样SQL可以为您的查询使用索引. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |