Note that there are some explanatory texts on larger screens.

plurals
  1. POWord boundaries (\b) in NSPredicate causing NSFetchRequest to return no managed objects
    text
    copied!<p>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.: <code>image.imageTitle = @"Issue 12: Special Edition";</code></p> <p>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 <code>imageAtIndex:</code> 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!</p> <p><em>Slow Code:</em></p> <pre><code> // Seek forward from the next page to the right for (i = currentPage + 1; i &lt; [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] &amp;&amp; keepLooking) { for (i = 0 ; i &lt; currentPage; i++) { iterationString = [[self imageAtIndex:i] imageTitle]; iterationNumber = [[iterationString stringByTrimmingCharactersInSet:nonDigits] intValue]; if (issueNumber == iterationNumber) { keepLooking = NO; break; } } } </code></pre> <p>Hoping for a much more efficient solution, I decided to try making a direct query on Core Data like so:</p> <pre><code> 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:&amp;error]; // [fetchedImages count] == 0 </code></pre> <p>Between the <a href="http://developer.apple.com/mac/library/documentation/cocoa/conceptual/Predicates/Articles/pSyntax.html#//apple_ref/doc/uid/TP40001795-SW1" rel="nofollow noreferrer">Predicate Programming Guide</a> and the <a href="http://userguide.icu-project.org/strings/regexp" rel="nofollow noreferrer">ICU regex specs</a>, 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!</p> <p>On the other hand, if I leave off the word boundaries and search instead for <code>stringWithFormat:@"%@",issueNumber</code>, I get dozens of managed objects returned, from 12 to 129 to 412.</p> <p>My best guess at this point is that I've run into one of Core Data's <a href="http://developer.apple.com/mac/library/documentation/cocoa/conceptual/Predicates/Articles/pBasics.html#//apple_ref/doc/uid/TP40001792-249799" rel="nofollow noreferrer">Constraints and Limitations</a>. If not, what am I doing wrong? If so, is there a workaround that offers both an exact match <em>and</em> the speed of a single fetch?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload