gpt4 book ai didi

iOS 核心数据。分组依据和掩码

转载 作者:行者123 更新时间:2023-11-30 12:56:49 26 4
gpt4 key购买 nike

我需要在Core Data中传输SQL查询:

SELECT `quadkey`, MIN(id) AS id, COUNT(id) AS count, AVG(longitude) AS longitude, AVG(latitude) AS latitude
FROM marker
GROUP BY (quadkey & 15499683151872);

我的代码:

NSFetchRequest *fetchRequest = [NSFetchRequest fetchRequestWithEntityName:@"GBUserRoute"];
NSExpressionDescription *countExpressionDescription = [self expressionForFunction:@"count:" arguments:@[[NSExpression expressionForKeyPath:@"routeId"]] resultName:@"count" resultType:NSDoubleAttributeType];

NSExpressionDescription *minIdExpressionDescription = [self expressionForFunction:@"min:" arguments:@[[NSExpression expressionForKeyPath:@"routeId"]] resultName:@"minId" resultType:NSDoubleAttributeType];

NSExpressionDescription *averageLatitudeExpressionDescription = [self expressionForFunction:@"average:" arguments:@[[NSExpression expressionForKeyPath:@"latitude"]] resultName:@"latitude" resultType:NSDoubleAttributeType];

NSExpressionDescription *averageLongitudeExpressionDescription = [self expressionForFunction:@"average:" arguments:@[[NSExpression expressionForKeyPath:@"longitude"]] resultName:@"longitude" resultType:NSDoubleAttributeType];

NSExpressionDescription *minLongitudeExpressionDescription = [self expressionForFunction:@"min:" arguments:@[[NSExpression expressionForKeyPath:@"longitude"]] resultName:@"minLongitude" resultType:NSDoubleAttributeType];

NSExpressionDescription *minLatitudeExpressionDescription = [self expressionForFunction:@"min:" arguments:@[[NSExpression expressionForKeyPath:@"latitude"]] resultName:@"minLatitude" resultType:NSDoubleAttributeType];

NSExpressionDescription *maxLongitudeExpressionDescription = [self expressionForFunction:@"max:" arguments:@[[NSExpression expressionForKeyPath:@"longitude"]] resultName:@"maxLongitude" resultType:NSDoubleAttributeType];

NSExpressionDescription *maxLatitudeExpressionDescription = [self expressionForFunction:@"max:" arguments:@[[NSExpression expressionForKeyPath:@"latitude"]] resultName:@"maxLatitude" resultType:NSDoubleAttributeType];

NSExpressionDescription *groupExpressionDescription = [self expressionForFunction:@"bitwiseAnd:with:" arguments:@[[NSExpression expressionForKeyPath:@"quadKey"], [NSExpression expressionForConstantValue:@(15499683151872)]] resultName:@"quadKeyMask" resultType:NSInteger64AttributeType];

[fetchRequest setPropertiesToFetch:@[groupExpressionDescription, countExpressionDescription, minIdExpressionDescription, averageLongitudeExpressionDescription, averageLatitudeExpressionDescription, minLongitudeExpressionDescription, minLatitudeExpressionDescription, maxLongitudeExpressionDescription, maxLatitudeExpressionDescription]];
[fetchRequest setPropertiesToGroupBy:@[groupExpressionDescription]];
[fetchRequest setResultType:NSDictionaryResultType];

+ (NSExpressionDescription *)expressionForFunction:(NSString *)function arguments:(NSArray<NSExpression *> *)arguments resultName:(NSString *)resultName resultType:(NSAttributeType)resultType {
NSExpression *expression = [NSExpression expressionForFunction: function
arguments: arguments];
NSExpressionDescription *expressionDescription = [[NSExpressionDescription alloc] init];
[expressionDescription setName: resultName];
[expressionDescription setExpression: expression];
[expressionDescription setExpressionResultType: resultType];

return expressionDescription;
}

运行此代码时,出现以下错误:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Invalid keypath expression ((), name quadKeyMask, isOptional 1, isTransient 0, entity (null), renamingIdentifier quadKeyMask, validation predicates ( ), warnings ( ), versionHashModifier (null) userInfo { }) passed to setPropertiesToFetch:'

最佳答案

首先,如果没有看到您的数据模型,我们实际上无法帮助您调试查询。

您的错误:

Invalid keypath expression ((), name quadKeyMask, isOptional 1, isTransient 0, entity (null), renamingIdentifier quadKeyMask, validation predicates ( ), warnings ( ), versionHashModifier (null) userInfo { }) passed to setPropertiesToFetch:

表示关键路径解释器无法遵循您的数据模型。 IOW,您的实体或表达式中有错误。

在 Core Data 中重写 SQL 查询的关键是要了解它们是非常不同的语言。核心数据谓词是使用过滤条件的集合选择操作。虽然这看起来与 SQL 声明性语言类似,但它是不同的。概念模型不同。

我的建议:将您的查询分成小部分重写。就像调试任何其他问题一样。让小片段发挥作用;然后让你的表达方式进行小的组合。分而治之。

关于iOS 核心数据。分组依据和掩码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40319754/

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