I have a large collection of CoreData objects which represent files. Periodically, I need to search this collection and find files which exist at a given path. Currently, I’m constructing the following NSPredicate and performing a fairly basic executeFetchRequest to find all results which match my query.
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];
The issue I’m having is that this function is called fairly frequently and the executeFetchRequest is accounting for a significant percentage of execution time (Instruments tells me the function accounts for around 49.2% in total, with 98.1% of that total spent on the executeFetchRequest call). I’m using NSSQLiteStoreType for my persistent store and have the path attribute indexed in my MOM.
My question is, how can I optimise this? I’ve considered setting up a lowercasePath attribute, and ditching the [c] modifier on the comparison, but am unsure as to what impact (if any) that would have on execution time.
Any help would be greatly appreciated.
Removing the
[c]should make a difference.==[c]is probably translated to an SQLiteLIKEoperator for which SQLite cannot use indexes, if I remember correctly. Speaking of which, do you have the attribute marked as indexed in your model? If not, you should do so, but again, with a case-insensitive search, this probably doesn’t make a difference.