简体   繁体   English

像iPhone联系人应用程序这样的核心数据搜索?

[英]Core Data search like iPhone Contacts app?

I have a Contact : NSManagedObject . 我有一个Contact : NSManagedObject I want to search all contacts by name (full name). 我想按name (全名)搜索所有联系人。 The search should perform like that of iPhone's Contacts app. 搜索的效果应与iPhone的“通讯录”应用类似。 So, name matches searchString if every word in searchString begins with any word in name . 因此, name匹配searchString ,如果在每一个字searchString以任何字开头name The search is case & diacritic insensitive. 该搜索不区分大小写和变音符号。

Eg, name "Matt Di Pasquale" matches searchString "Matt Pa", "Matt Mat", and "Pasq Di má" but does not match "att" or "squale". 例如, name “ Matt Di Pasquale”与searchString “ Matt Pa”,“ Matt Mat”和“ Pasq Dimá”匹配,但与“ att”或“ squale”不匹配。

UPDATE: Watch the WWDC 2010 Session Video: Optimizing Core Data Performance on iPhone OS for a much faster way to do this. 更新:观看WWDC 2010会话视频:在iPhone OS上优化核心数据性能,这是一种更快的方法。

Based on another answer about NSPredicate , create an NSCompoundPredicate from subpredicates using an ICU regular expression : 根据关于NSPredicate的另一个答案 ,使用ICU正则表达式从子谓词创建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];

I think MATCHES filtering happens after the objects have been fetched into memory, so name CONTAINS[cd] %@ should limit the number of fetched objects and perhaps speed things up. 我认为MATCHES过滤是在将对象提取到内存之后进行的,因此name CONTAINS[cd] %@应该限制​​所提取对象的数量,并可能加快处理速度。

(1) Cocoa Plant implements -[NSString words] (1) 可可工厂实现了-[NSString words]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM