フェッチ リクエストで NSExpression を使用する最初の試みでは、通常のフェッチ リクエストを使用した場合とは一貫して異なる結果が得られます。
MO「サブジェクト」はMO「ブック」と対多の関係にあり、逆は対1です。
これは私が使用している NSExpression fetchRequest です:
Project_AppDelegate *appDelegate = [[NSApplication sharedApplication] delegate];
NSManagedObjectContext *context = [appDelegate managedObjectContext];
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@“Book”
inManagedObjectContext:context];
Subject *subjectToDelete = [self.arrayOfSubjects objectAtIndex:indexSelected];
NSPredicate *pred = [NSPredicate predicateWithFormat:@"subject == %@", subjectToDelete];
NSExpression *expn = [NSExpression expressionForFunction:@"count:"
arguments:[NSArray arrayWithObject:[NSExpression expressionForKeyPath:@"idPerm"]]];
NSExpressionDescription *expnDesc = [[NSExpressionDescription alloc] init];
[expnDesc setExpression:expn];
[expnDesc setName:@“countMatchingBooks”];
[expnDesc setExpressionResultType:NSInteger64AttributeType];
NSArray *properties = [NSArray arrayWithObject:expnDesc];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
[request setPredicate:pred];
[request setPropertiesToFetch:properties];
[request setResultType:NSDictionaryResultType];
NSError *error = nil;
NSArray *results = [context executeFetchRequest:request error:&error];
if (error) {
// error handling here
}
[request release];
[expnDesc release];
// Retrieve the count from the results array.
NSNumber *numBooksAssignedSubjectToDelete = [[results objectAtIndex:0] valueForKey:@“countMatchingBooks”];
uint64_t uloloBooksAssignedSubjectToDelete = [numBooksAssignedSubjectToDelete unsignedLongLongValue];
(アイデアは、ユーザーが選択したサブジェクトを削除することを選択した場合に、カスケード ルールを介して削除されるブックの数をユーザーに通知する確認パネルをユーザーに提示することです — この時点でブック MO を失敗させることはありません)。
そして、これは私がテストとして使用している単純な fetchRequest です:
NSEntityDescription *entityDescription = [NSEntityDescription entityForName:@“Book”
inManagedObjectContext:contextMain];
NSFetchRequest *request = [[NSFetchRequest alloc] init];
[request setEntity:entityDescription];
NSError *error = nil;
NSArray *booksAll = [contex executeFetchRequest:request error:&error];
[request release];
// Loop through the “booksAll” array and count those whose subject matches the one assigned as “subjectToDelete”
NSExpression fetchRequest が n のカウントを返す場合、単純な fetchRequest は n + 1 のカウントを返します。
fetchRequests 自体が何らかの形でデータを変更している可能性があると考えて、別の順序で実行してみましたが、結果は同じでした。
式を使用したリクエストは、まだ保存されていない MO をスキップするのでしょうか? いいえ。式リクエストと通常のリクエストのギャップが広がるかどうかを確認するために、多数の新しい「Book」MO を作成するテストを実行しました。まさにワンオフのままでした。
私が間違っていることは何か分かりますか?