2013-05-27 2 views
12

를 사용하여 일대 다 관계를 필터링 나는이 내 핵심 데이터 모델에서 다음과 같은 두 기관 :코어 데이터 - 술어

Manufacturer.manufactures <------>> Product.manufacturedBy 

I :

Manufacture {name, ...other attributes} 
Product {name, .... other attributes} 

내가 가지고있는 설정 많은 관계로 하나 검색 문자열과 일치하는 Manufacturer에 속한 모든 제품을 반환하는 조건자를 작성하려고합니다. 예 : 두 개의 제조사, "King Nut"및 "Queen Nut"이있는 경우 "Nut"에 대한 검색은 King Nut 및 Queen Nut에서 만든 모든 제품을 반환해야합니다.

필자 내 조건자는 내 필터가 Product 엔터티에있을 때 완벽하게 작동하지만 Manufacturer 엔터티에서 필터링 할 때 어떤 조건 자도 작동하지 않습니다. 결과 세트가 비어 있습니다.

predicate = [NSPredicate predicateWithFormat:@"manufacturedBy.name CONTAINS[cd] %@",searchValue]; 
    predicate = [NSPredicate predicateWithFormat:@"manufacturedBy.name like %@",wildcardString]; 
    predicate = [NSPredicate predicateWithFormat:@"manufacturedBy.name matches %@",wildcardString]; 
    predicate = [NSPredicate predicateWithFormat:@"ALL manufacturedBy.name like %@",wildcardString]; 
    predicate = [NSPredicate predicateWithFormat:@"ALL manufacturedBy.name like[cd] %@",@wildcardString]; 
+0

당신은'무엇을 의미합니까? 'Product's를 가져 오려고하면 요청 엔터티가'Product'이어야합니다 –

+0

예. 필자의 엔터티 요청은 Product : –

답변

4

애플에서 this 문서를 살펴 보자 :

NSEntityDescription *entity = [NSEntityDescription entityForName:@"Product" inManagedObjectContext:[GBKDB context]]; 
searchValue = @"nut"; 
NSString *wildcardString = [NSString stringWithFormat:@"*%@*", searchValue]; 

나는 다음과 같은 노력했다. "ANY"술어로 수행하려고 시도하는 예제를 수행합니다.

NSString* searchVal = @"nut"; 
NSFetchRequest* r = [[NSFetchRequest alloc] initWithEntityName:@"Product"]; 
[r setPredicate:[NSPredicate predicateWithFormat:@"manufacturedBy.name CONTAINS[cd] %@",searchVal]]; 

가 같아야합니다 "너트"요청을 포함하는 제품 이름 Manufacturer의를 얻으려면 : 같은

+0

이 문서에는 'Matt *' "]와 같은 ANY employees.firstName과 % @", wildcardedString];와 같은 ANY employees.firstName이 있습니다. 첫 번째 예제에서는 작은 따옴표가 있고 두 번째 예제에서는 작은 따옴표가 있습니다. 작은 따옴표를 사용해야합니까 (wildcardString에는 작은 따옴표가 포함되어서는 안됩니다) –

+0

wildcardedString = @ "Matt *" – Rambatino

+1

문서가 만료되었다고 가정하면 ... 정확히 –

18

하는 이름이 "너트"를 포함하여 제조 업체에 의해 제조 Product의를 얻으려면, 귀하의 요청이 보일 것입니다 : 당신의 결과 집합이 비어있는 경우

NSString* searchVal = @"nut"; 
NSFetchRequest* r = [[NSFetchRequest alloc] initWithEntityName:@"Manufacture"]; 
[r setPredicate:[NSPredicate predicateWithFormat:@"ANY manufactures.name CONTAINS[cd] %@",searchVal]]; 

, 그것으로 인해 어떤 물체가 술어 (문자열 "너트"를 포함)을 대답하지 않는다는 사실 수 있습니다.
알려진 이름과 테스트로 가짜 엔티티를 추가해보십시오.

편집 : 이 당신이 테스트에 사용할 수있는 코드 :` "제조업체 엔티티에 필터링 할 때"

typedef void(^config_block_t)(id); 

- (void) synthesizeObjectsOfEntity:(NSString*)entity 
          context:(NSManagedObjectContext*)context 
          count:(NSUInteger)count 
         configBlock:(config_block_t)configBlock 
{ 
    for (;count;--count) { 
     NSManagedObject* object = [NSEntityDescription insertNewObjectForEntityForName:entity 
                   inManagedObjectContext:context]; 
     configBlock(object); 
    } 
} 

- (void) synthesizeProductsAndManufacturersInContext:(NSManagedObjectContext*)context 
{ 
    NSMutableArray* manufacturers = [NSMutableArray new]; 
    [self synthesizeObjectsOfEntity:@"Manufactur" 
          context:context 
           count:10 
         configBlock:^(Manufactur* m) { 
          m.name = [NSString stringWithFormat:@"m-%u%u%u",arc4random()%10,arc4random()%10,arc4random()%10]; 
          [manufacturers addObject:m]; 
         }]; 
    [self synthesizeObjectsOfEntity:@"Product" 
          context:context 
           count:100 
         configBlock:^(Product* p) { 
          p.name = [NSString stringWithFormat:@"p-%u%u%u",arc4random()%10,arc4random()%10,arc4random()%10]; 
          p.manufacturedBy = manufacturers[arc4random() % [manufacturers count]]; 
         }]; 
    [context save:NULL]; 
    [context reset]; 
    NSString* searchVal = @"3"; 
    NSFetchRequest* r = [[NSFetchRequest alloc] initWithEntityName:@"Product"]; 
    [r setPredicate:[NSPredicate predicateWithFormat:@"manufacturedBy.name CONTAINS[cd] %@",searchVal]]; 
    NSArray* match = [context executeFetchRequest:r error:NULL]; 
    NSLog(@"matched: %u",[match count]); 
} 
+0

첫 번째 예제를 사용하여 제품을 반환하려고합니다 ... 여전히 작동하지 않습니다! 내 데이터베이스가 비어 있지 않습니다, 나는 데이터를 볼 수있는 DBVisualizer 있습니다. –

+0

'Manufactur'의'name' 속성을 올바르게 설정 했습니까 (변환 가능합니까, 재정의 했습니까)? –

+0

주 : contains 연산자는 ANY 연산자와 작동하지 않으므로 contains 연산자 (예 : ANY employees.firstName에 'Matthew'포함)를 사용할 수 없습니다. – jose920405