NSDictionaryResultType 표현식은 새로 삽입 된 객체를 고려하지 않습니다
-
06-07-2019 - |
문제
코어 데이터에서 필드의 최대 값을 원하므로 다음 요청을 설정했습니다.
// Create fetch
NSFetchRequest *fetch = [[NSFetchRequest alloc] init];
[fetch setEntity:[NSEntityDescription entityForName:@"MyEntity" inManagedObjectContext:context]];
[fetch setResultType:NSDictionaryResultType];
// Expression for Max ID
NSExpression *keyPathExpression = [NSExpression expressionForKeyPath:@"myNumbericID"];
NSExpression *minExpression = [NSExpression expressionForFunction:@"max:" arguments:[NSArray arrayWithObject:keyPathExpression]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"maxID"];
[expressionDescription setExpression:minExpression];
[expressionDescription setExpressionResultType:NSDoubleAttributeType];
[fetch setPropertiesToFetch:[NSArray arrayWithObject:expressionDescription]];
// Execute the fetch.
double theID = 0;
NSError *error;
NSArray *objects = [context executeFetchRequest:fetch error:&error];
if (objects && [objects count] > 0) {
theID = [((NSNumber *)[[objects objectAtIndex:0] valueForKey:@"maxID"]) doubleValue];
}
그러나 미리 컨텍스트에 삽입 된 새로운 객체를 고려하지 않은 것 같습니다. 이것이 어떻게 작동해야합니까? 상점의 물체에서만 작동합니까?
문서에서 어떤 언급도 찾을 수 없습니다.
감사,
마이크
해결책
Apple Developer Forums의 응답을 받았으며 구축되지 않은 변경 사항을 고려하지 않음이 확인되었습니다.
다른 팁
이 동작은 [nsfetchRequest setIncludEpendingChanges :]에 문서화됩니다. NSDictionaryResultType에서는 예의 값이 지원되지 않습니다. 이것은 구원받지 않은 변경에서 nsexpressiondescription을 사용할 수 없음을 의미합니다.
참고로, 전체 nsexpression 섹션을 건너 뛰고 Fetch를 계속 진행 한 다음 최대 값을 위해 수집 연산자를 사용하십시오.
theID = [objects valueForKeyPath:"@max.myNumbericID"];
제휴하지 않습니다 StackOverflow