-1
두 모델 사이에 일대일 관계가있는 Practice와 PracticeRecord가 있습니다. 이제 관련 practiceRecord 객체의 수가 최소이지만 0보다 큰 Practice 객체의 배열을 가져와야합니다. 나는 코드 아래에 썼지 만 작동하지 않았고 요청을 가져올 때 예외가 발생했습니다. 아무나 제발 우아한 솔루션을 제공 할 수 있습니다. 미리 감사드립니다.코어 데이터의 다 대다 하위 모델 수의 최소값 가져 오는 방법
NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Practice"];
request.predicate = [NSPredicate predicateWithFormat:@"[email protected] > 0"];
[request setResultType:NSDictionaryResultType];
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"[email protected]"];
NSExpression *minExpression = [NSExpression expressionForFunction:@"min:" arguments:[NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"minPracticeRecordTimes"];
[expressionDescription setExpression:minExpression];
[expressionDescription setExpressionResultType:NSDecimalAttributeType];
[request setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];
NSError *error = nil;
NSArray *objects = [context executeFetchRequest:request error:&error];
if (objects == nil) {
// Handle the error.
}
else {
if ([objects count] > 0) {
NSLog(@"Minimum practice record times: %@", [[objects objectAtIndex:0]
valueForKey:@"minPracticeRecordTimes"]);
}
}