Word boundaries (\b) in NSPredicate causing NSFetchRequest to return no managed objects
Using Core Data w/a sqlite store on iPhone.... I've got a bunch of comic book image entities, each with a string that includes the comic's issue#, e.g.: image.imageTitle = @"Issue 12: Special Edition";
Part of the UI allows the user to type in an issue number to jump to the next issue. My initial code for this was sloooooooow because imageAtIndex:
queries Core Data for one object at a time. Over several hundred issues, it could take upwards of 40 seconds just to get through the first loop!
Slow Code:
// Seek forward from the next page to the right
for (i = currentPage + 1; i < [self numberOfPages]; i++) {
iterationString = [[self imageAtIndex:i] imageTitle];
iterationNumber = [[iterationString stringByTrimmingCharactersInSet:nonDigits] intValue];
if (issueNumber == iterationNumber) {
keepLooking = NO;
break;
}
}
// If nothing was found to the right, seek forward from 0 to the current page
if (i == [self numberOfPages] && keepLooking) {
for (i = 0 ; i < currentPage; i++) {
iterationString = [[self imageAtIndex:i] imageTitle];
iterationNumber = [[iterationString stringByTrimmingCharactersInSet:nonDigits] intValue];
if (issueNumber == iterationNumber) {
keepLooking = NO;
break;
}
}
}
Hoping for a much more efficient solution, I decided to try making a direct query on Core Data like so:
NSString *issueNumber = @"12";
NSString *issueWithWordBoundaries = [NSString stringWithFormat:@"\\b%@\\b",issueNumber];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(groupID == %@) AND (imageTitle CONTAINS %@)", groupID, issueWithWordBoundaries];
NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
NSEntityDescription *entity = [NSEntityDescription entityForName:@"CBImage" inManagedObjectContext:self.managedObjectContext];
[fetchRequest setEntity:entity];
[fetchRequest setPredicate:predicate];
[fetchRequest setIncludesSubentities:NO]; // Not sure if this is needed, but just in case....
// Execute the fetch
NSError *error = nil;
NSArray *fetchedObjects = [managedObjectContext executeFetchRequest:fetchRequest error:&error];
// [fetchedImages count] == 0
Between the Predicate Programming Guide and the ICU regex specs, I figured the \b's would help prevent a search for 12 returning 120, 121, 122, etc. Instead, it doesn't return anything from the store at all!
On the other hand, if I leave off the word boundaries and search instead for stringWithFormat:@"%@",issueNumber
, I get dozens of managed objects returned, from 12 to 129 to 412.
My best guess at this point is that I've run into one of Core Data's Constraints and Limitations. If not, what am I doing wrong? If so, is there a wor开发者_C百科karound that offers both an exact match and the speed of a single fetch?
\b
refers to a backspace character. Try
[NSString stringWithFormat:@"\\b%@\\b",issueNumber];
// ^^ ^^
And to perform RegEx match, use MATCHES, not CONTAINS.
Turns out the word boundaries were a red herring: My problem was a variant on this issue: The regex couldn't succeed without some wild cards to match the bits I didn't care about in the imageTitles.
The general case solution to finding an exact phrase using NSPredicate is therefore:
NSString *exactPhrase = @"phrase_you_hope_to_find_in_another_string";
NSString *regularExpression = [NSString stringWithFormat:@".*\\b%@\\b.*",exactPhrase];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"stringToSearch CONTAINS %@", exactPhrase];
// Assuming a string or an entity's string attribute named stringToSearch
精彩评论