2

나는 Contact : NSManagedObject이 있습니다. 모든 연락처를 name (전체 이름)으로 검색하고 싶습니다. 검색은 iPhone의 주소록 앱과 비슷하게 수행되어야합니다. 따라서 searchString의 모든 단어가 name에있는 단어로 시작하는 경우 namesearchString과 일치합니다. 검색은 대소 문자 구별이없는 경우 &입니다.iPhone 연락처 앱과 같은 핵심 데이터 검색?

예 : name "Matt Di Pasquale"은 searchString "Matt Pa", "Matt Mat"및 "Pasq Di má"와 일치하지만 "att"또는 "squale"과 일치하지 않습니다.

답변

1

업데이트 :이를 수행하는 데 훨씬 빠른 방법으로 WWDC 2010 Session Video: Optimizing Core Data Performance on iPhone OS을 시청하십시오.

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] %@를 가져올 객체와 아마 속도의 수를 제한해야합니다

another answer about NSPredicate을 바탕으로, ICU regular expression 사용 subpredicates에서 NSCompoundPredicate를 만들 일들.

(1) Cocoa Plant implements -[NSString words]

관련 문제