我有一个预填充的 Coredata 文件,我想在 iOS 应用程序中进行搜索。该文件有这样的索引词~(70,000 行)~10 MB 文件。
ad
adam
arm
apple
..
..
zen
zener
我已经从文件中构建了 Coredata 并查询了 SQLLite 例如:给我所有以“a”开头的单词。在模拟器上搜索大约需要 300 毫秒,但在 iPad 设备上大约需要 2 秒或更长时间?我怎样才能让它更快?其他搜索引擎类型的应用程序如何在设备上执行此操作?
//////////////////////////////////////////////////////////////////////
//This will get list of words begining with letters from search word
//////////////////////////////////////////////////////////////////////
-(void) FetchWords
{
NSString *entityName=@"KeyWord";
NSString *sortKey = @"word";
NSArray *fetchColumns = [[NSArray alloc] initWithObjects:@"word", nil];
//init fetch request
NSFetchRequest *req = [[NSFetchRequest alloc] init];
NSString *query = self.searchBar.text;
//
//split search phrase into words - searches words with any order and not case senitive - remove last space if any
//
NSMutableArray *words = [[query componentsSeparatedByString:@" "] mutableCopy];
if([words[words.count-1] isEqualToString:@""])
[words removeObjectAtIndex:[words count]-1];
if(query.length)
{
NSMutableArray *subpredicates = [NSMutableArray array];
if(words.count >1)
{
[self GetMatchingCategoryCodes];
// NSLog(@"%@",categories);
for (NSString *code in categories)
{
//intersection between last word and previous categories already filtered for previous words in search
NSPredicate *pred = [NSPredicate predicateWithFormat:@"word BEGINSWITH[cd] %@ AND code BEGINSWITH [cd] %@",words[words.count-1],code];
[subpredicates addObject:pred];
}
req.predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subpredicates];
}
else
{
for(NSString *token in words)
{
NSPredicate *pred = [NSPredicate predicateWithFormat:@"word BEGINSWITH[cd] %@",token];
[subpredicates addObject:pred];
}
req.predicate = [NSCompoundPredicate orPredicateWithSubpredicates:subpredicates];
}
}
//setup request
NSEntityDescription *entity = [NSEntityDescription entityForName:entityName inManagedObjectContext:context];
[req setEntity:entity];
[req setFetchBatchSize:100];
//sort descriptors
NSSortDescriptor *desc = [[NSSortDescriptor alloc] initWithKey:sortKey ascending:YES];
NSArray *descriptors = [NSArray arrayWithObject:desc];
[req setSortDescriptors:descriptors];
//for unique values - ignore repeatition
req.propertiesToFetch = fetchColumns;
req.returnsDistinctResults = YES;
req.resultType = NSDictionaryResultType;
//execute request
NSError *error;
Codes = [context executeFetchRequest:req error:&error];
// NSLog(@"Fetched=%d",Codes.count);
}