我的核心数据模型有一个 Note 实体,与 Link 实体有多对多关系,可通过 Note 上的 links 属性访问 .

我正在尝试获取Core Data中每个音符的 links 属性的计数 . 为此,我按照下面的链接示例,但我得到了意想不到的结果 . Chained expressions to perform calculations in Core Data

以下是我如何配置获取请求和相关表达式描述:

// Create the expression description that should return the link count for each note.
NSExpression *linksExpression = [NSExpression expressionForKeyPath:@"links"];
NSExpression *countExpression = [NSExpression expressionForFunction:@"count:" arguments:@[linksExpression]];
NSExpressionDescription *linkCountExpressionDescription = [[NSExpressionDescription alloc] init];
linkCountExpressionDescription.name = @"linkCount";
linkCountExpressionDescription.expression = countExpression;
linkCountExpressionDescription.expressionResultType = NSInteger32AttributeType;

// Execute a fetch request that should return an array of dictionaries, each with a single "linkCount" key.
NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"Note"];
fetchRequest.resultType = NSDictionaryResultType;
fetchRequest.propertiesToFetch = @[linkCountExpressionDescription];
NSArray *countsArray = [self.managedObjectContext executeFetchRequest:fetchRequest error:nil];

我正在使用的SQLite存储包含1000个注释,因此我希望获得1000个字典的数组,每个字典包含 LinkCount 键及其 links 关系的计数 . 相反,我得到一个字典,给我所有链接的总数 .

我应该采取哪些不同的方式来获取每个音符的链接数?