gpt4 book ai didi

ios - 核心数据 : Fetch count of to-many relationship with NSDictionaryResultType

转载 作者:塔克拉玛干 更新时间:2023-11-01 19:09:21 25 4
gpt4 key购买 nike

我在 Core Data 中有一个很大的对象列表(大约 50 000 个并且会定期增加)。我通过以下请求获取它:

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] initWithEntityName:[SongObject name]];
fetchRequest.sortDescriptors = @[[NSSortDescriptor sortDescriptorWithKey:@"name" ascending:YES]];
fetchRequest.propertiesToFetch = @[@"uid", @"name", @"toArtistRef.uid", @"toArtistRef.name"];
fetchRequest.resultType = NSDictionaryResultType;

实体 SongObject 还包含关系 toSongsInPlaylistRef,这是一对多的。

我需要为每个对象获取此集合的计数。由于数据量大,我无法获取关系本身。我尝试添加@"toSongsInPlaylistRef.@count"@"toSongsInPlaylistRef.count"但是它崩溃了

Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: 'Invalid to many relationship in setPropertiesToFetch: (toSongsInPlaylistRef.count)'

请写下任何建议。

最佳答案

要获取相关对象的计数,您必须创建一个NSExpressionDescription并将其包含在 propertiesToFetch 中:

NSExpression *countExpression = [NSExpression expressionForFunction:@"count:"
arguments:@[[NSExpression expressionForKeyPath:@"toSongsInPlaylistRef"]]];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName:@"playListCount"]; // Choose an appropriate name here!
[expressionDescription setExpression:countExpression];
[expressionDescription setExpressionResultType:NSInteger32AttributeType];

fetchRequest.propertiesToFetch = @[expressionDescription, ...];

关于ios - 核心数据 : Fetch count of to-many relationship with NSDictionaryResultType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19160032/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com