NSPredicate中的字边界(\b)导致NSFetchRequest不返回任何受管对象 [英] Word boundaries (\b) in NSPredicate causing NSFetchRequest to return no managed objects

查看:136
本文介绍了NSPredicate中的字边界(\b)导致NSFetchRequest不返回任何受管对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Core Data在iPhone上使用sqlite存储....我有一堆漫画图像实体,每个都有一个包含漫画问题#的字符串,例如: image。 imageTitle = @Issue 12:Special Edition;

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";

部分UI允许用户输入问题编号跳转到下一期。我的初始代码是sloooooowow,因为 imageAtIndex:一次查询一个对象的核心数据。超过几百个问题,可能需要40秒才能完成第一个循环!

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!

慢速代码:

 // 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;
   }
  }
 }

解决方案,我决定尝试直接查询Core Data,如下所示:

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

href =http://developer.apple.com/mac/library/documentation/cocoa/conceptual/Predicates/Articles/pSyntax.html#//apple_ref/doc/uid/TP40001795-SW1 =nofollow noreferrer>谓词编程指南和 ICU正则表达式规范,我想出了\ b会帮助防止搜索12返回120,121,122等,而不是从商店返回任何东西!

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!

另一方面,如果我离开单词边界并搜索 stringWithFormat:@%@,issueNumber ,我得到几十个管理对象返回,从12到129到412。

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.

我最好的猜测是,我遇到了Core Data的限制和限制。如果不是,我做错了什么?

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 workaround that offers both an exact match and the speed of a single fetch?

推荐答案

如果是,是否有一个解决方法,提供完全匹配

找出字词边界是一个红色的鲱鱼:我的问题是此问题的变体:正则表达式不能成功,没有一些通配符匹配的位,我不在乎imageTitles。

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.

一般情况下解决方案使用NSPredicate因此:

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

这篇关于NSPredicate中的字边界(\b)导致NSFetchRequest不返回任何受管对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆