【问题标题】:Core Data search like iPhone Contacts app?像 iPhone 通讯录应用一样的核心数据搜索?
【发布时间】:2012-01-23 02:26:06
【问题描述】:

我有一个Contact : NSManagedObject。我想通过name(全名)搜索所有联系人。搜索应该像 iPhone 的联系人应用程序那样执行。因此,如果searchString 中的每个单词都以name 中的任何单词开头,那么name 匹配searchString。搜索不区分大小写和变音符号。

例如,name“Matt Di Pasquale”匹配 searchString“Matt Pa”、“Matt Mat”和“Pasq Di má”,但不匹配“att”或“squale”。

【问题讨论】:

    标签: iphone regex core-data nspredicate nsfetchedresultscontroller


    【解决方案1】:

    更新:观看WWDC 2010 Session Video: Optimizing Core Data Performance on iPhone OS 了解更快的方法。

    基于another answer about NSPredicate,使用ICU regular expression从子谓词创建NSCompoundPredicate

    NSArray *searchWords = [searchString words]; // see link below (1)
    NSMutableArray *subpredicates = [NSMutableArray arrayWithCapacity:[searchWords count]];
    for (NSString *searchWord in searchWords) {
        [subpredicates addObject:[NSPredicate predicateWithFormat:
                                  @"name CONTAINS[cd] %@ AND" // maybe speeds it up
                                  " name MATCHES[cd] %@",
                                  searchWord, [NSString stringWithFormat:
                                               @".*\\b%@.*", searchWord]]];
    }
    fetchRequest.predicate = [NSCompoundPredicate andPredicateWithSubpredicates:subpredicates];
    

    我认为MATCHES 过滤发生在对象被提取到内存之后,所以name CONTAINS[cd] %@ 应该限制提取对象的数量并可能加快处理速度。

    (1)Cocoa Plant implements -[NSString words]

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多