NSDictionaryResultType式は、新しく挿入されたオブジェクトを考慮していません

StackOverflow https://stackoverflow.com/questions/1632029

  •  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:]で(不十分に)文書化されています。 YESの値は、NSDictionaryResultTypeではサポートされていません。これは、保存されていない変更に対してNSExpressionDescriptionを使用できないことを意味します。

FYI、NSExpressionセクション全体をスキップしてフェッチを続行し、最大値にコレクション演算子を使用できます。

theID = [objects valueForKeyPath:"@max.myNumbericID"];
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top