iOS CoreData。分组和掩码

时间:2016-10-29 13:51:14

标签: ios objective-c swift core-data

我需要在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;
}

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

  

由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'无效的keypath表达式((),名称quadKeyMask,isOptional 1,isTransient 0,entity(null),renamingIdentifier quadKeyMask,验证谓词(   ),警告(   ),versionHashModifier(null)    用户信息 {   传递给setPropertiesToFetch:'

1 个答案:

答案 0 :(得分:0)

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

您的错误:

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声明性语言类似,但它是不同的。概念模型是不同的。

我的建议:用小块重写你的查询。就像你要调试任何其他问题一样。使小件工作;然后将表达式的小组合工作。分而治之。