iOS アプリで検索したい事前設定された Coredata ファイルがあります。ファイルには、次のような単語が索引付けされています ~ (70,000 行) ~10 MB のファイル。
ad
adam
arm
apple
..
..
zen
zener
ファイルから Coredata を作成し、SQLLite にクエリを実行しました。たとえば、「a」で始まるすべての単語を教えてください。シミュレーターだと検索に300msecくらいかかるのに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);
}