0

어쩌면 하위 쿼리를 사용하여 가능할지는 모르겠지만 특정 속성 집합과 일치하는 데이터 저장소의 모든 항목을 가져 오는 가져 오기 요청을 설정하고 싶습니다. 간단하지만 다른 기준 세트와 일치하는 개체의 수를 제한하고 싶습니다.NSFetchedResultsController가있는 NSPredicate는 데이터 저장소에서 모두 가져 오지만 특정 일치를 제한합니까?

나는 자신의 어레이를 롤링하고 FRC가 잘 작동하는 것과 비교하여 NSFetchedResultscontroller를 사용할 수 있기를 원합니다.

제가 작업 한 프로젝트와 관련되어 있고 "너무 많이 제공하지 않으려"하고 싶은 예제를 사용하겠습니다. 기본 엔티티 예 :

테이프

  • (문자열) 이름
  • (관계) 제조 업체
  • (부울) isTransparent
  • (부울) IsEmpty 함수

코드 :

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Tape"]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"manufacturer = %@", manufacturer]; 
request.predicate = predicate 
request.sortDescriptors = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]; 

NSFetchedResultsController frc = [[NSFetchedResultsController alloc] initWithFetchRequest:request managedObjectContext:moc sectionNameKeyPath:@"isTransparent" cacheName:nil]; 

FRC는 이름을 사용하여 사전 순으로 정렬하고 isTransparent 플래그를 사용하여 섹션을 구분합니다 (잘 작동 함).

isEmpty 플래그로 표시된 저장소에 여러 개의 개체가있을 수 있습니다. 그러나 사용자에게 표시 될 때 주어진 시간과 같이 isEmpty 플래그로 표시된 표시 만하고 싶습니다.

그래서, 우리와 함께 가게를 가질 수있는 다음

  • 알파 테이프 (IsEmpty 함수 = 0, isTransparent = 1)
  • 베타 테이프 (IsEmpty 함수 = 0, isTransparent = 0)
  • 감마 테이프 (IsEmpty 함수 = 1, isTransparent = 1)
  • 델타 테이프 (IsEmpty 함수 = 1, isTransparent = 0) 현재의 조건에서

, 우리가 얻을 것 탭 이런 제작 :

  • 알파 테이프 (IsEmpty 함수 = 0 isTransparent = 1)
  • 감마 테이프 (IsEmpty 함수 = 1 isTransparent = 1)
  • 구역 나누기
  • 베타 테이프 (IsEmpty 함수 = 0, isTransparent = 0)
  • 델타 테이프 (IsEmpty 함수 = 1, isTransparent = 0)

내가 궁금하면가 가능하다면입니다

  • 알파 테이프 (IsEmpty 함수 = 0, isTransparent = 1)
  • 감마 테이프 (IsEmpty 함수 = 1, isTransparent = 1)
  • : (일치하는 FRC 포함) 테이블 뷰에 대해이 같은 것을 얻을 구역 나누기
  • 베타 테이프 (IsEmpty 함수 = 0 isTransparent = 0) 테이블 뷰는 isTransparent 플래그로 구획하지만, 단지 isEmpt 표시된 하나의 객체가 표시된다

y 플래그.

다음 추가 코드가 도움이 될 것입니다. 이것은 내가 배열을 (신속하고 더러운주의)를 사용하여이 작업을 수행하는 방법이다 : IsEmpty 함수는 YES 제조 업체와 함께 일하기,

NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Tape"]; 
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"manufacturer = %@", manufacturer]; 
request.predicate = predicate 
request.sortDescriptors = [NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)]; 

BOOL foundEmpty = NO; 
NSMutableArray *sectionOne = [[NSMutableArray alloc] init]; 
NSMutableArray *sectionTwo = [[NSMutableArray alloc] init]; 

NSArray *result = [self.managedObjectContext executeFetchRequest:request error:nil]; 
for (Tape *tape in result) { 
    if (tape.isEmpty.boolValue && !foundEmpty && tape.isTransparent.boolValue) { 
     // only include one isEmpty object 
     [sectionOne addObject:tape]; 
     foundEmpty = YES; 

    } else if (tape.isEmpty.boolValue && !foundEmpty && !tape.isTransparent.boolValue) { 
     // only include one isEmpty object 
     [sectionOne addObject:tape]; 
     foundEmpty = YES; 

    } else if (tape.isTransparent.boolValue && !tape.isEmpty.boolValue) { 
     // always include non-isEmpty objects 
     [sectionOne addObject:tape]; 

    } else if (!tape.isEmpty.boolValue) { 
     // always include non-isEmpty objects 
     [sectionTwo addObject:tape]; 

    } 
} 

NSArray self.dataSource = @[sectionOnce, sectionTwo]; 
+0

문제가 무엇인지 확실하지 않습니다. FRC가 여러 항목을 가질 수 있지만 최대 하나만 표시하려고하는 섹션을 원하십니까? – Wain

+0

@Wain - 추가 세부 정보. 설명을 요청 해 주셔서 감사합니다. –

답변

0

는 아래의 조건을

NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(manufacturer = %@) AND (NOT (isTransparent = 0 AND isEmpty = 1)))", manufacturer]; 

위의 코드 검사를 시도

+0

Wain의 의견을 바탕으로 질문에 대한 자세한 내용을 추가했습니다. 제안 된 현재 술어에서는 isEmpty로 플래그 된 플래그 만 반환한다고 생각합니다. isEmpty로 플래그되지 않은 것을 포함하지 않습니다.그 맞습니까; 또는, 나는 무엇인가 놓치고있다. –

+0

@JoshBruce 필터링 된 결과 델타 테이프가 누락되었습니다. 따라서 isTransparent가 0 일 때 isEmpty가 0 인 테이프를 가져 오는 것이 조건입니까? – Adithya

+0

설명을 기반으로 답변이 업데이트되었습니다. – Adithya

0

간단히 말하면 테이블 위임 메서드에 약간의 논리를 추가하기 만하면됩니다. 현재 FRC에서 섹션 행 수를 반환합니다. 행이없는 경우, 아무것도 표시되지 않습니다

MIN(1, rowCountFromFRC) 

있도록,하지만이있는 경우 (1)의 최대 표시됩니다 대신, 해당 부분에 대한 반환합니다.

+0

이렇게하면 항상 한 행만 표시됩니까? 위의 확장 예제를 참조하십시오. –

+0

무슨 소리 야? 이것은 1 행 또는 0 행을 표시합니다. – Wain

+0

예제에서는 3 행, 2 섹션 및 isEmpty = 1 ...와 함께 하나의 엔티티를 보여줍니다. 최종 테이블보기 예제 목록 ... 현재 조건자는 2 개의 섹션이있는 4 개의 행을 표시합니다. 희망을 명확히하는 데 도움이됩니다. –

관련 문제