2012-10-12 2 views
2

코어 데이터 C.사용 자 NSPredicate는 I 세 엔티티 A, B가

이들 사이의 관계는이 오브젝트를 필터링하려면 다음 < - >> B, B < -> C한다.

A에는 '유형'이라는 속성이 있습니다.

A 및 B 관계는 a2b이고 B 및 C 관계는 b2c입니다. c_array는 C 객체의 목록입니다.

NSPredicate를 사용하여 A를 C로 필터링하고 A의 특성 'type'을 (를) 필터링하려고합니다.

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc]init]; 

NSEntityDescription *entity = [NSEntityDescription entityForName:@"A" inManagedObjectContext:managedObjectContext]; 
[fetchRequest setEntity:entity]; 

NSMutableArray *parr = [NSMutableArray array]; 

for (C *c in c_array) { 
    [parr addObject:[NSPredicate predicateWithFormat:@"ANY a2b.b2c = %@", c]]; 
} 

NSPredicate *predicate = [NSCompoundPredicate andPredicateWithSubpredicates:[NSArray arrayWithObjects:[NSCompoundPredicate orPredicateWithSubpredicates:parr], [NSPredicate predicateWithFormat:@"type = %i", 0], nil]]; 

[fetchRequest setPredicate:predicate]; 

하지만 내가 예상 한 것은 아닙니다. 그래서 나는 다른 것을 시도했다.

predicate = [NSPredicate predicateWithFormat:@"type=%i AND (0!=SUBQUERY(a2b,$a2b,$a2b.b2c IN %@)[email protected])", 0, c_array]; 

예기치 않은 결과가 다시 발생했습니다. 누군가 나를 도울 수 있습니까? T T

답변

0

다른 방식으로 듣고 싶습니다. 당신은 이미 C를 가지고 있고 당신이 기술 한 관계에 기초하여 C 클래스는 B 속성을 가질 것이고 B 클래스는 A 속성을 가질 것입니다. 그래서 당신은 c2b 사용했습니다과 같은 b2a 뭔가 작업을해야 가정 :

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"c2b.b2a.type == %@",[NSNumber numberWithInt:0]]; 

NSArray *result = [c_array filteredArrayUsingPredicate:predicate]; 

또한 당신이 당신의 조건에 int로 Atype 특성을 비교하고 확인할 수 있습니다. type==%i을 찾고 i가 0이면 type에 값이없는 모든 개체를 반환합니다. 따라서 typeNSNumber 개체 또는 type.intValue int와 비교하십시오.

+0

답장을 보내 주셔서 감사합니다.) 줄 단위로 확인하여이 문제를 해결했습니다. 나는 B를 추가 할 때 C와 B 사이에 하나의 관계를 놓친다는 것을 알았습니다. 내 어리석은 실수에 대해 유감으로 생각합니다. 하지만 당신 NSNumber 문제를 지적, 그건 나에게 도움이 감사합니다. – brianLikeApple

관련 문제